From 9299935e43ba37b59f01b5eb6909e5fc99d0a935 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Fri, 24 Apr 2020 22:02:58 +0200 Subject: [PATCH 1/6] async_io: implement IntoRawSocket and IntoRawFd --- src/async_io.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/async_io.rs b/src/async_io.rs index 298bb4d0..a0451c97 100644 --- a/src/async_io.rs +++ b/src/async_io.rs @@ -8,13 +8,13 @@ use std::future::Future; use std::io::{self, Read, Write}; use std::net::{SocketAddr, TcpListener, TcpStream, ToSocketAddrs, UdpSocket}; #[cfg(windows)] -use std::os::windows::io::{AsRawSocket, RawSocket}; +use std::os::windows::io::{AsRawSocket, IntoRawSocket, RawSocket}; use std::pin::Pin; use std::sync::Arc; use std::task::{Context, Poll}; #[cfg(unix)] use std::{ - os::unix::io::{AsRawFd, RawFd}, + os::unix::io::{AsRawFd, IntoRawFd, RawFd}, os::unix::net::{SocketAddr as UnixSocketAddr, UnixDatagram, UnixListener, UnixStream}, path::Path, }; @@ -153,6 +153,13 @@ impl AsRawFd for Async { } } +#[cfg(unix)] +impl IntoRawFd for Async { + fn into_raw_fd(self) -> RawFd { + self.source.raw + } +} + #[cfg(windows)] impl Async { /// Creates an async I/O handle. @@ -203,6 +210,13 @@ impl AsRawSocket for Async { } } +#[cfg(windows)] +impl IntoRawSocket for Async { + fn into_raw_socket(self) -> RawSocket { + self.source.raw + } +} + impl Async { /// Gets a reference to the inner I/O handle. /// From c94f74249ca393c46bb25ed3819446eae9e40d00 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Fri, 24 Apr 2020 22:03:08 +0200 Subject: [PATCH 2/6] task: return JoinHandle from detach --- src/task.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/task.rs b/src/task.rs index f9f5055c..36210f3c 100644 --- a/src/task.rs +++ b/src/task.rs @@ -176,7 +176,7 @@ where } } -impl Task<()> { +impl Task { /// Detaches the task to let it keep running in the background. /// /// # Examples @@ -195,12 +195,10 @@ impl Task<()> { /// .detach(); /// # }) /// ``` - pub fn detach(mut self) { - self.0.take().unwrap(); + pub fn detach(mut self) -> async_task::JoinHandle { + self.0.take().unwrap() } -} -impl Task { /// Cancels the task and waits for it to stop running. /// /// Returns the task's output if it was completed just before it got canceled, or `None` if it From b81bc1c2af6c3ed1c1b4b67ff5ca9e82494cc923 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Sat, 25 Apr 2020 16:49:25 +0200 Subject: [PATCH 3/6] fix into_raw and add from_raw --- src/async_io.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/async_io.rs b/src/async_io.rs index a0451c97..aae5c53d 100644 --- a/src/async_io.rs +++ b/src/async_io.rs @@ -14,7 +14,7 @@ use std::sync::Arc; use std::task::{Context, Poll}; #[cfg(unix)] use std::{ - os::unix::io::{AsRawFd, IntoRawFd, RawFd}, + os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}, os::unix::net::{SocketAddr as UnixSocketAddr, UnixDatagram, UnixListener, UnixStream}, path::Path, }; @@ -156,7 +156,15 @@ impl AsRawFd for Async { #[cfg(unix)] impl IntoRawFd for Async { fn into_raw_fd(self) -> RawFd { - self.source.raw + self.into_inner().unwrap().into_raw_fd() + } +} + +#[cfg(unix)] +impl FromRawFd for Async { + unsafe fn from_raw_fd(fd: RawFd) -> Self { + let raw = T::from_raw_fd(fd); + Async::new(raw).expect("invalid file descriptor") } } @@ -213,7 +221,7 @@ impl AsRawSocket for Async { #[cfg(windows)] impl IntoRawSocket for Async { fn into_raw_socket(self) -> RawSocket { - self.source.raw + self.into_inner().unwrap().into_raw_socket() } } From 258147d15c62530e84472f0dfa5868733c28863d Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Sat, 25 Apr 2020 18:44:22 +0200 Subject: [PATCH 4/6] undo detach change, implment Into for Task --- src/task.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/task.rs b/src/task.rs index 36210f3c..0596e4ff 100644 --- a/src/task.rs +++ b/src/task.rs @@ -176,7 +176,7 @@ where } } -impl Task { +impl Task<()> { /// Detaches the task to let it keep running in the background. /// /// # Examples @@ -195,10 +195,12 @@ impl Task { /// .detach(); /// # }) /// ``` - pub fn detach(mut self) -> async_task::JoinHandle { - self.0.take().unwrap() + pub fn detach(mut self) { + self.0.take().unwrap(); } +} +impl Task { /// Cancels the task and waits for it to stop running. /// /// Returns the task's output if it was completed just before it got canceled, or `None` if it @@ -252,3 +254,11 @@ impl Future for Task { } } } + +impl Into> for Task { + fn into(mut self) -> async_task::JoinHandle { + self.0 + .take() + .expect("task was already canceled or has failed") + } +} From 0d6ea14e5c289e3d7313b66d8e847f89bee95619 Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Sat, 25 Apr 2020 19:01:24 +0200 Subject: [PATCH 5/6] remove unneeded FromRawFd Co-Authored-By: Stjepan Glavina --- src/async_io.rs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/async_io.rs b/src/async_io.rs index aae5c53d..f044013f 100644 --- a/src/async_io.rs +++ b/src/async_io.rs @@ -159,15 +159,6 @@ impl IntoRawFd for Async { self.into_inner().unwrap().into_raw_fd() } } - -#[cfg(unix)] -impl FromRawFd for Async { - unsafe fn from_raw_fd(fd: RawFd) -> Self { - let raw = T::from_raw_fd(fd); - Async::new(raw).expect("invalid file descriptor") - } -} - #[cfg(windows)] impl Async { /// Creates an async I/O handle. From 9b44f7f839baaf8d2ca542458956915924bc8379 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Sat, 25 Apr 2020 19:02:10 +0200 Subject: [PATCH 6/6] remove unused import --- src/async_io.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/async_io.rs b/src/async_io.rs index f044013f..d8d887fc 100644 --- a/src/async_io.rs +++ b/src/async_io.rs @@ -14,7 +14,7 @@ use std::sync::Arc; use std::task::{Context, Poll}; #[cfg(unix)] use std::{ - os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}, + os::unix::io::{AsRawFd, IntoRawFd, RawFd}, os::unix::net::{SocketAddr as UnixSocketAddr, UnixDatagram, UnixListener, UnixStream}, path::Path, };