From dd126c2333a2382e83e8b676413db8d8ec29e79b Mon Sep 17 00:00:00 2001 From: Denis Date: Wed, 26 Jun 2019 18:51:38 +0300 Subject: [PATCH] Implement `TryFrom` to transform various I/O primitives into their mio counterparts (#1158) * `TryFrom for mio::net::TcpListener` * `TryFrom for mio::net::TcpStream` * `TryFrom for mio::net::UdpSocket` * `TryFrom for mio_uds::UnixListener` * `TryFrom for mio_uds::UnixStream` --- tokio-tcp/src/listener.rs | 13 +++++++++++++ tokio-tcp/src/stream.rs | 13 +++++++++++++ tokio-udp/src/socket.rs | 13 +++++++++++++ tokio-uds/src/listener.rs | 13 +++++++++++++ tokio-uds/src/stream.rs | 13 +++++++++++++ 5 files changed, 65 insertions(+) diff --git a/tokio-tcp/src/listener.rs b/tokio-tcp/src/listener.rs index 39a7af7de..2f639a66c 100644 --- a/tokio-tcp/src/listener.rs +++ b/tokio-tcp/src/listener.rs @@ -2,6 +2,7 @@ use super::incoming::Incoming; use super::TcpStream; use mio; +use std::convert::TryFrom; use std::fmt; use std::io; use std::net::{self, SocketAddr}; @@ -313,6 +314,18 @@ impl TcpListener { } } +impl TryFrom for mio::net::TcpListener { + 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: TcpListener) -> Result { + value.io.into_inner() + } +} + 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 46f17837f..68ab150e3 100644 --- a/tokio-tcp/src/stream.rs +++ b/tokio-tcp/src/stream.rs @@ -1,6 +1,7 @@ use bytes::{Buf, BufMut}; use iovec::IoVec; use mio; +use std::convert::TryFrom; use std::fmt; use std::future::Future; use std::io; @@ -709,6 +710,18 @@ impl TcpStream { } } +impl TryFrom for mio::net::TcpStream { + 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: TcpStream) -> Result { + value.io.into_inner() + } +} + // ===== impl Read / Write ===== impl AsyncRead for TcpStream { diff --git a/tokio-udp/src/socket.rs b/tokio-udp/src/socket.rs index 765018da4..09d20ee5c 100644 --- a/tokio-udp/src/socket.rs +++ b/tokio-udp/src/socket.rs @@ -1,6 +1,7 @@ use super::{RecvDgram, SendDgram}; use futures::{try_ready, Async, Poll}; use mio; +use std::convert::TryFrom; use std::fmt; use std::io; use std::net::{self, Ipv4Addr, Ipv6Addr, SocketAddr}; @@ -418,6 +419,18 @@ impl UdpSocket { } } +impl TryFrom for mio::net::UdpSocket { + 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: UdpSocket) -> Result { + value.io.into_inner() + } +} + 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/listener.rs b/tokio-uds/src/listener.rs index 865197ec4..cdfeacea0 100644 --- a/tokio-uds/src/listener.rs +++ b/tokio-uds/src/listener.rs @@ -2,6 +2,7 @@ use crate::{Incoming, UnixStream}; use futures::{try_ready, Async, Poll}; use mio::Ready; use mio_uds; +use std::convert::TryFrom; use std::fmt; use std::io; use std::os::unix::io::{AsRawFd, RawFd}; @@ -130,6 +131,18 @@ impl UnixListener { } } +impl TryFrom for mio_uds::UnixListener { + 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: UnixListener) -> Result { + value.io.into_inner() + } +} + 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 b14b0e07e..3521e1a66 100644 --- a/tokio-uds/src/stream.rs +++ b/tokio-uds/src/stream.rs @@ -5,6 +5,7 @@ use iovec::{self, IoVec}; use libc; use mio::Ready; use mio_uds; +use std::convert::TryFrom; use std::fmt; use std::io::{self, Read, Write}; use std::net::Shutdown; @@ -127,6 +128,18 @@ impl UnixStream { } } +impl TryFrom for mio_uds::UnixStream { + 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: UnixStream) -> Result { + value.io.into_inner() + } +} + impl Read for UnixStream { fn read(&mut self, buf: &mut [u8]) -> io::Result { self.io.read(buf)