diff --git a/tokio-fs/src/file/mod.rs b/tokio-fs/src/file/mod.rs index 9e5ffbd9d..d3cae8b55 100644 --- a/tokio-fs/src/file/mod.rs +++ b/tokio-fs/src/file/mod.rs @@ -18,6 +18,7 @@ pub use self::seek::SeekFuture; use tokio_io::{AsyncRead, AsyncWrite}; +use std::convert::TryFrom; use std::fs::{File as StdFile, Metadata, Permissions}; use std::io::{self, Read, Seek, Write}; use std::path::Path; @@ -536,3 +537,19 @@ impl Drop for File { } } } + +impl From for File { + fn from(std: StdFile) -> Self { + Self::from_std(std) + } +} + +impl TryFrom for StdFile { + type Error = io::Error; + + fn try_from(mut file: File) -> Result { + file.std + .take() + .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "`File` instance already shutdown")) + } +} diff --git a/tokio-tcp/src/listener.rs b/tokio-tcp/src/listener.rs index c5b765244..4eb39a654 100644 --- a/tokio-tcp/src/listener.rs +++ b/tokio-tcp/src/listener.rs @@ -351,6 +351,18 @@ impl TryFrom for mio::net::TcpListener { } } +impl TryFrom for TcpListener { + type Error = io::Error; + + /// Consumes stream, returning the tokio I/O object. + /// + /// This is equivalent to + /// [`TcpListener::from_std(stream, &Handle::default())`](TcpListener::from_std). + fn try_from(stream: net::TcpListener) -> Result { + Self::from_std(stream, &Handle::default()) + } +} + impl fmt::Debug for TcpListener { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.io.get_ref().fmt(f) diff --git a/tokio-tcp/src/stream.rs b/tokio-tcp/src/stream.rs index bf7dfe2e1..978e42fbe 100644 --- a/tokio-tcp/src/stream.rs +++ b/tokio-tcp/src/stream.rs @@ -886,6 +886,18 @@ impl TryFrom for mio::net::TcpStream { } } +impl TryFrom for TcpStream { + type Error = io::Error; + + /// Consumes stream, returning the tokio I/O object. + /// + /// This is equivalent to + /// [`TcpStream::from_std(stream, &Handle::default())`](TcpStream::from_std). + fn try_from(stream: net::TcpStream) -> Result { + Self::from_std(stream, &Handle::default()) + } +} + // ===== impl Read / Write ===== impl AsyncRead for TcpStream { diff --git a/tokio-udp/src/socket.rs b/tokio-udp/src/socket.rs index 215836423..9779004f0 100644 --- a/tokio-udp/src/socket.rs +++ b/tokio-udp/src/socket.rs @@ -451,6 +451,18 @@ impl TryFrom for mio::net::UdpSocket { } } +impl TryFrom for UdpSocket { + type Error = io::Error; + + /// Consumes stream, returning the tokio I/O object. + /// + /// This is equivalent to + /// [`UdpSocket::from_std(stream, &Handle::default())`](UdpSocket::from_std). + fn try_from(stream: net::UdpSocket) -> Result { + Self::from_std(stream, &Handle::default()) + } +} + impl fmt::Debug for UdpSocket { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.io.get_ref().fmt(f) diff --git a/tokio-uds/src/datagram.rs b/tokio-uds/src/datagram.rs index 6213a625a..f36299ad8 100644 --- a/tokio-uds/src/datagram.rs +++ b/tokio-uds/src/datagram.rs @@ -2,6 +2,7 @@ use crate::{Recv, RecvFrom, Send, SendTo}; use futures_core::ready; use mio::Ready; use mio_uds; +use std::convert::TryFrom; use std::fmt; use std::io; use std::net::Shutdown; @@ -312,6 +313,30 @@ impl UnixDatagram { } } +impl TryFrom for mio_uds::UnixDatagram { + type Error = io::Error; + + /// Consumes value, returning the mio I/O object. + /// + /// See [`tokio_reactor::PollEvented::into_inner`] for more details about + /// resource deregistration that happens during the call. + fn try_from(value: UnixDatagram) -> Result { + value.io.into_inner() + } +} + +impl TryFrom for UnixDatagram { + type Error = io::Error; + + /// Consumes stream, returning the tokio I/O object. + /// + /// This is equivalent to + /// [`UnixDatagram::from_std(stream, &Handle::default())`](UnixDatagram::from_std). + fn try_from(stream: net::UnixDatagram) -> Result { + Self::from_std(stream, &Handle::default()) + } +} + impl fmt::Debug for UnixDatagram { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.io.get_ref().fmt(f) diff --git a/tokio-uds/src/listener.rs b/tokio-uds/src/listener.rs index 4769516ad..22ce198d1 100644 --- a/tokio-uds/src/listener.rs +++ b/tokio-uds/src/listener.rs @@ -155,6 +155,18 @@ impl TryFrom for mio_uds::UnixListener { } } +impl TryFrom for UnixListener { + type Error = io::Error; + + /// Consumes stream, returning the tokio I/O object. + /// + /// This is equivalent to + /// [`UnixListener::from_std(stream, &Handle::default())`](UnixListener::from_std). + fn try_from(stream: net::UnixListener) -> io::Result { + Self::from_std(stream, &Handle::default()) + } +} + impl fmt::Debug for UnixListener { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.io.get_ref().fmt(f) diff --git a/tokio-uds/src/stream.rs b/tokio-uds/src/stream.rs index 5c1eef74c..0a8d33ad0 100644 --- a/tokio-uds/src/stream.rs +++ b/tokio-uds/src/stream.rs @@ -130,6 +130,18 @@ impl TryFrom for mio_uds::UnixStream { } } +impl TryFrom for UnixStream { + type Error = io::Error; + + /// Consumes stream, returning the tokio I/O object. + /// + /// This is equivalent to + /// [`UnixStream::from_std(stream, &Handle::default())`](UnixStream::from_std). + fn try_from(stream: net::UnixStream) -> io::Result { + Self::from_std(stream, &Handle::default()) + } +} + impl AsyncRead for UnixStream { unsafe fn prepare_uninitialized_buffer(&self, _: &mut [u8]) -> bool { false