diff options
author | Carl Lerche <me@carllerche.com> | 2020-10-02 13:54:00 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-02 13:54:00 -0700 |
commit | 1e585ccb516c8dc7c13cbc3d50f8ca49260b9617 (patch) | |
tree | 00959b4ac82e4972314baa043cdbca2f2ebf5848 /tokio/src/net/unix | |
parent | 7ec6d88b21ea3e5531176f526a51dae0a4513025 (diff) |
io: update to Mio 0.7 (#2893)
This also makes Mio an implementation detail, removing it from the
public API.
This is based on #1767.
Diffstat (limited to 'tokio/src/net/unix')
-rw-r--r-- | tokio/src/net/unix/datagram/socket.rs | 50 | ||||
-rw-r--r-- | tokio/src/net/unix/listener.rs | 44 | ||||
-rw-r--r-- | tokio/src/net/unix/mod.rs | 3 | ||||
-rw-r--r-- | tokio/src/net/unix/socketaddr.rs | 31 | ||||
-rw-r--r-- | tokio/src/net/unix/stream.rs | 26 |
5 files changed, 79 insertions, 75 deletions
diff --git a/tokio/src/net/unix/datagram/socket.rs b/tokio/src/net/unix/datagram/socket.rs index 78baf279..20c6c227 100644 --- a/tokio/src/net/unix/datagram/socket.rs +++ b/tokio/src/net/unix/datagram/socket.rs @@ -1,11 +1,12 @@ use crate::io::PollEvented; +use crate::net::unix::SocketAddr; use std::convert::TryFrom; use std::fmt; use std::io; use std::net::Shutdown; use std::os::unix::io::{AsRawFd, RawFd}; -use std::os::unix::net::{self, SocketAddr}; +use std::os::unix::net; use std::path::Path; cfg_uds! { @@ -77,7 +78,7 @@ cfg_uds! { /// # } /// ``` pub struct UnixDatagram { - io: PollEvented<mio_uds::UnixDatagram>, + io: PollEvented<mio::net::UnixDatagram>, } } @@ -107,7 +108,7 @@ impl UnixDatagram { where P: AsRef<Path>, { - let socket = mio_uds::UnixDatagram::bind(path)?; + let socket = mio::net::UnixDatagram::bind(path)?; UnixDatagram::new(socket) } @@ -141,7 +142,7 @@ impl UnixDatagram { /// # } /// ``` pub fn pair() -> io::Result<(UnixDatagram, UnixDatagram)> { - let (a, b) = mio_uds::UnixDatagram::pair()?; + let (a, b) = mio::net::UnixDatagram::pair()?; let a = UnixDatagram::new(a)?; let b = UnixDatagram::new(b)?; @@ -183,12 +184,12 @@ impl UnixDatagram { /// # } /// ``` pub fn from_std(datagram: net::UnixDatagram) -> io::Result<UnixDatagram> { - let socket = mio_uds::UnixDatagram::from_datagram(datagram)?; + let socket = mio::net::UnixDatagram::from_std(datagram); let io = PollEvented::new(socket)?; Ok(UnixDatagram { io }) } - fn new(socket: mio_uds::UnixDatagram) -> io::Result<UnixDatagram> { + fn new(socket: mio::net::UnixDatagram) -> io::Result<UnixDatagram> { let io = PollEvented::new(socket)?; Ok(UnixDatagram { io }) } @@ -225,7 +226,7 @@ impl UnixDatagram { /// # } /// ``` pub fn unbound() -> io::Result<UnixDatagram> { - let socket = mio_uds::UnixDatagram::unbound()?; + let socket = mio::net::UnixDatagram::unbound()?; UnixDatagram::new(socket) } @@ -298,7 +299,7 @@ impl UnixDatagram { /// ``` pub async fn send(&self, buf: &[u8]) -> io::Result<usize> { self.io - .async_io(mio::Ready::writable(), |sock| sock.send(buf)) + .async_io(mio::Interest::WRITABLE, |sock| sock.send(buf)) .await } @@ -397,7 +398,7 @@ impl UnixDatagram { /// ``` pub async fn recv(&self, buf: &mut [u8]) -> io::Result<usize> { self.io - .async_io(mio::Ready::readable(), |sock| sock.recv(buf)) + .async_io(mio::Interest::READABLE, |sock| sock.recv(buf)) .await } @@ -467,7 +468,7 @@ impl UnixDatagram { P: AsRef<Path>, { self.io - .async_io(mio::Ready::writable(), |sock| { + .async_io(mio::Interest::WRITABLE, |sock| { sock.send_to(buf, target.as_ref()) }) .await @@ -507,9 +508,12 @@ impl UnixDatagram { /// # } /// ``` pub async fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> { - self.io - .async_io(mio::Ready::readable(), |sock| sock.recv_from(buf)) - .await + let (n, addr) = self + .io + .async_io(mio::Interest::READABLE, |sock| sock.recv_from(buf)) + .await?; + + Ok((n, SocketAddr(addr))) } /// Try to receive data from the socket without waiting. @@ -545,7 +549,8 @@ impl UnixDatagram { /// # } /// ``` pub fn try_recv_from(&mut self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> { - self.io.get_ref().recv_from(buf) + let (n, addr) = self.io.get_ref().recv_from(buf)?; + Ok((n, SocketAddr(addr))) } /// Returns the local address that this socket is bound to. @@ -589,7 +594,7 @@ impl UnixDatagram { /// # } /// ``` pub fn local_addr(&self) -> io::Result<SocketAddr> { - self.io.get_ref().local_addr() + self.io.get_ref().local_addr().map(SocketAddr) } /// Returns the address of this socket's peer. @@ -638,7 +643,7 @@ impl UnixDatagram { /// # } /// ``` pub fn peer_addr(&self) -> io::Result<SocketAddr> { - self.io.get_ref().peer_addr() + self.io.get_ref().peer_addr().map(SocketAddr) } /// Returns the value of the `SO_ERROR` option. @@ -701,23 +706,14 @@ impl UnixDatagram { } } -impl TryFrom<UnixDatagram> for mio_uds::UnixDatagram { - type Error = io::Error; - - /// Consumes value, returning the mio I/O object. - fn try_from(value: UnixDatagram) -> Result<Self, Self::Error> { - value.io.into_inner() - } -} - -impl TryFrom<net::UnixDatagram> for UnixDatagram { +impl TryFrom<std::os::unix::net::UnixDatagram> for UnixDatagram { type Error = io::Error; /// Consumes stream, returning the Tokio I/O object. /// /// This is equivalent to /// [`UnixDatagram::from_std(stream)`](UnixDatagram::from_std). - fn try_from(stream: net::UnixDatagram) -> Result<Self, Self::Error> { + fn try_from(stream: std::os::unix::net::UnixDatagram) -> Result<Self, Self::Error> { Self::from_std(stream) } } diff --git a/tokio/src/net/unix/listener.rs b/tokio/src/net/unix/listener.rs index dc8cb08e..5d586ec3 100644 --- a/tokio/src/net/unix/listener.rs +++ b/tokio/src/net/unix/listener.rs @@ -1,12 +1,12 @@ use crate::future::poll_fn; use crate::io::PollEvented; -use crate::net::unix::{Incoming, UnixStream}; +use crate::net::unix::{Incoming, SocketAddr, UnixStream}; use std::convert::TryFrom; use std::fmt; use std::io; use std::os::unix::io::{AsRawFd, RawFd}; -use std::os::unix::net::{self, SocketAddr}; +use std::os::unix::net; use std::path::Path; use std::task::{Context, Poll}; @@ -46,7 +46,7 @@ cfg_uds! { /// } /// ``` pub struct UnixListener { - io: PollEvented<mio_uds::UnixListener>, + io: PollEvented<mio::net::UnixListener>, } } @@ -64,7 +64,7 @@ impl UnixListener { where P: AsRef<Path>, { - let listener = mio_uds::UnixListener::bind(path)?; + let listener = mio::net::UnixListener::bind(path)?; let io = PollEvented::new(listener)?; Ok(UnixListener { io }) } @@ -83,14 +83,14 @@ impl UnixListener { /// from a future driven by a tokio runtime, otherwise runtime can be set /// explicitly with [`Runtime::enter`](crate::runtime::Runtime::enter) function. pub fn from_std(listener: net::UnixListener) -> io::Result<UnixListener> { - let listener = mio_uds::UnixListener::from_listener(listener)?; + let listener = mio::net::UnixListener::from_std(listener); let io = PollEvented::new(listener)?; Ok(UnixListener { io }) } /// Returns the local socket address of this listener. pub fn local_addr(&self) -> io::Result<SocketAddr> { - self.io.get_ref().local_addr() + self.io.get_ref().local_addr().map(SocketAddr) } /// Returns the value of the `SO_ERROR` option. @@ -111,24 +111,15 @@ impl UnixListener { &mut self, cx: &mut Context<'_>, ) -> Poll<io::Result<(UnixStream, SocketAddr)>> { - let (io, addr) = ready!(self.poll_accept_std(cx))?; - - let io = mio_uds::UnixStream::from_stream(io)?; - Ok((UnixStream::new(io)?, addr)).into() - } - - fn poll_accept_std( - &mut self, - cx: &mut Context<'_>, - ) -> Poll<io::Result<(net::UnixStream, SocketAddr)>> { loop { let ev = ready!(self.io.poll_read_ready(cx))?; - match self.io.get_ref().accept_std() { - Ok(None) => { - self.io.clear_readiness(ev); + match self.io.get_ref().accept() { + Ok((sock, addr)) => { + let addr = SocketAddr(addr); + let sock = UnixStream::new(sock)?; + return Poll::Ready(Ok((sock, addr))); } - Ok(Some((sock, addr))) => return Ok((sock, addr)).into(), Err(ref err) if err.kind() == io::ErrorKind::WouldBlock => { self.io.clear_readiness(ev); } @@ -192,23 +183,14 @@ impl crate::stream::Stream for UnixListener { } } -impl TryFrom<UnixListener> for mio_uds::UnixListener { - type Error = io::Error; - - /// Consumes value, returning the mio I/O object. - fn try_from(value: UnixListener) -> Result<Self, Self::Error> { - value.io.into_inner() - } -} - -impl TryFrom<net::UnixListener> for UnixListener { +impl TryFrom<std::os::unix::net::UnixListener> for UnixListener { type Error = io::Error; /// Consumes stream, returning the tokio I/O object. /// /// This is equivalent to /// [`UnixListener::from_std(stream)`](UnixListener::from_std). - fn try_from(stream: net::UnixListener) -> io::Result<Self> { + fn try_from(stream: std::os::unix::net::UnixListener) -> io::Result<Self> { Self::from_std(stream) } } diff --git a/tokio/src/net/unix/mod.rs b/tokio/src/net/unix/mod.rs index b079fe04..21aa4fe7 100644 --- a/tokio/src/net/unix/mod.rs +++ b/tokio/src/net/unix/mod.rs @@ -14,6 +14,9 @@ pub use split::{ReadHalf, WriteHalf}; mod split_owned; pub use split_owned::{OwnedReadHalf, OwnedWriteHalf, ReuniteError}; +mod socketaddr; +pub use socketaddr::SocketAddr; + pub(crate) mod stream; pub(crate) use stream::UnixStream; diff --git a/tokio/src/net/unix/socketaddr.rs b/tokio/src/net/unix/socketaddr.rs new file mode 100644 index 00000000..48f7b96b --- /dev/null +++ b/tokio/src/net/unix/socketaddr.rs @@ -0,0 +1,31 @@ +use std::fmt; +use std::path::Path; + +/// An address associated with a Tokio Unix socket. +pub struct SocketAddr(pub(super) mio::net::SocketAddr); + +impl SocketAddr { + /// Returns `true` if the address is unnamed. + /// + /// Documentation reflected in [`SocketAddr`] + /// + /// [`SocketAddr`]: std::os::unix::net::SocketAddr + pub fn is_unnamed(&self) -> bool { + self.0.is_unnamed() + } + + /// Returns the contents of this address if it is a `pathname` address. + /// + /// Documentation reflected in [`SocketAddr`] + /// + /// [`SocketAddr`]: std::os::unix::net::SocketAddr + pub fn as_pathname(&self) -> Option<&Path> { + self.0.as_pathname() + } +} + +impl fmt::Debug for SocketAddr { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(fmt) + } +} diff --git a/tokio/src/net/unix/stream.rs b/tokio/src/net/unix/stream.rs index 715ed7aa..d0f98f43 100644 --- a/tokio/src/net/unix/stream.rs +++ b/tokio/src/net/unix/stream.rs @@ -3,13 +3,14 @@ use crate::io::{AsyncRead, AsyncWrite, PollEvented, ReadBuf}; use crate::net::unix::split::{split, ReadHalf, WriteHalf}; use crate::net::unix::split_owned::{split_owned, OwnedReadHalf, OwnedWriteHalf}; use crate::net::unix::ucred::{self, UCred}; +use crate::net::unix::SocketAddr; use std::convert::TryFrom; use std::fmt; use std::io::{self, Read, Write}; use std::net::Shutdown; use std::os::unix::io::{AsRawFd, RawFd}; -use std::os::unix::net::{self, SocketAddr}; +use std::os::unix::net; use std::path::Path; use std::pin::Pin; use std::task::{Context, Poll}; @@ -21,7 +22,7 @@ cfg_uds! { /// from a listener with `UnixListener::incoming`. Additionally, a pair of /// anonymous Unix sockets can be created with `UnixStream::pair`. pub struct UnixStream { - io: PollEvented<mio_uds::UnixStream>, + io: PollEvented<mio::net::UnixStream>, } } @@ -35,7 +36,7 @@ impl UnixStream { where P: AsRef<Path>, { - let stream = mio_uds::UnixStream::connect(path)?; + let stream = mio::net::UnixStream::connect(path)?; let stream = UnixStream::new(stream)?; poll_fn(|cx| stream.io.poll_write_ready(cx)).await?; @@ -56,7 +57,7 @@ impl UnixStream { /// from a future driven by a tokio runtime, otherwise runtime can be set /// explicitly with [`Runtime::enter`](crate::runtime::Runtime::enter) function. pub fn from_std(stream: net::UnixStream) -> io::Result<UnixStream> { - let stream = mio_uds::UnixStream::from_stream(stream)?; + let stream = mio::net::UnixStream::from_std(stream); let io = PollEvented::new(stream)?; Ok(UnixStream { io }) @@ -68,26 +69,26 @@ impl UnixStream { /// communicating back and forth between one another. Each socket will /// be associated with the default event loop's handle. pub fn pair() -> io::Result<(UnixStream, UnixStream)> { - let (a, b) = mio_uds::UnixStream::pair()?; + let (a, b) = mio::net::UnixStream::pair()?; let a = UnixStream::new(a)?; let b = UnixStream::new(b)?; Ok((a, b)) } - pub(crate) fn new(stream: mio_uds::UnixStream) -> io::Result<UnixStream> { + pub(crate) fn new(stream: mio::net::UnixStream) -> io::Result<UnixStream> { let io = PollEvented::new(stream)?; Ok(UnixStream { io }) } /// Returns the socket address of the local half of this connection. pub fn local_addr(&self) -> io::Result<SocketAddr> { - self.io.get_ref().local_addr() + self.io.get_ref().local_addr().map(SocketAddr) } /// Returns the socket address of the remote half of this connection. pub fn peer_addr(&self) -> io::Result<SocketAddr> { - self.io.get_ref().peer_addr() + self.io.get_ref().peer_addr().map(SocketAddr) } /// Returns effective credentials of the process which called `connect` or `pair`. @@ -139,15 +140,6 @@ impl UnixStream { } } -impl TryFrom<UnixStream> for mio_uds::UnixStream { - type Error = io::Error; - - /// Consumes value, returning the mio I/O object. - fn try_from(value: UnixStream) -> Result<Self, Self::Error> { - value.io.into_inner() - } -} - impl TryFrom<net::UnixStream> for UnixStream { type Error = io::Error; |