Merge pull request #1014 from joshtriplett/convert-from-async-to-sync

Add `TryFrom` impls to convert async types to corresponding sync types
pull/1017/head
Yosh 3 years ago committed by GitHub
commit f8231d7e68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -252,6 +252,16 @@ impl From<std::net::TcpListener> for TcpListener {
}
}
impl std::convert::TryFrom<TcpListener> for std::net::TcpListener {
type Error = io::Error;
/// Converts a `TcpListener` into its synchronous equivalent.
fn try_from(listener: TcpListener) -> io::Result<std::net::TcpListener> {
let inner = listener.watcher.into_inner()?;
inner.set_nonblocking(false)?;
Ok(inner)
}
}
cfg_unix! {
use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};

@ -378,6 +378,21 @@ impl From<std::net::TcpStream> for TcpStream {
}
}
impl std::convert::TryFrom<TcpStream> for std::net::TcpStream {
type Error = io::Error;
/// Converts a `TcpStream` into its synchronous equivalent.
fn try_from(stream: TcpStream) -> io::Result<std::net::TcpStream> {
let inner = Arc::try_unwrap(stream.watcher)
.map_err(|_| io::Error::new(
io::ErrorKind::Other,
"Cannot convert TcpStream to synchronous: multiple references",
))?
.into_inner()?;
inner.set_nonblocking(false)?;
Ok(inner)
}
}
cfg_unix! {
use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};

@ -532,6 +532,16 @@ impl From<std::net::UdpSocket> for UdpSocket {
}
}
impl std::convert::TryFrom<UdpSocket> for std::net::UdpSocket {
type Error = io::Error;
/// Converts a `UdpSocket` into its synchronous equivalent.
fn try_from(listener: UdpSocket) -> io::Result<std::net::UdpSocket> {
let inner = listener.watcher.into_inner()?;
inner.set_nonblocking(false)?;
Ok(inner)
}
}
cfg_unix! {
use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};

@ -311,6 +311,16 @@ impl From<StdUnixDatagram> for UnixDatagram {
}
}
impl std::convert::TryFrom<UnixDatagram> for StdUnixDatagram {
type Error = io::Error;
/// Converts a `UnixDatagram` into its synchronous equivalent.
fn try_from(listener: UnixDatagram) -> io::Result<StdUnixDatagram> {
let inner = listener.watcher.into_inner()?;
inner.set_nonblocking(false)?;
Ok(inner)
}
}
impl AsRawFd for UnixDatagram {
fn as_raw_fd(&self) -> RawFd {
self.watcher.as_raw_fd()

@ -205,6 +205,16 @@ impl From<StdUnixListener> for UnixListener {
}
}
impl std::convert::TryFrom<UnixListener> for StdUnixListener {
type Error = io::Error;
/// Converts a `UnixListener` into its synchronous equivalent.
fn try_from(listener: UnixListener) -> io::Result<StdUnixListener> {
let inner = listener.watcher.into_inner()?;
inner.set_nonblocking(false)?;
Ok(inner)
}
}
impl AsRawFd for UnixListener {
fn as_raw_fd(&self) -> RawFd {
self.watcher.as_raw_fd()

@ -231,6 +231,21 @@ impl From<StdUnixStream> for UnixStream {
}
}
impl std::convert::TryFrom<UnixStream> for StdUnixStream {
type Error = io::Error;
/// Converts a `UnixStream` into its synchronous equivalent.
fn try_from(stream: UnixStream) -> io::Result<StdUnixStream> {
let inner = Arc::try_unwrap(stream.watcher)
.map_err(|_| io::Error::new(
io::ErrorKind::Other,
"Cannot convert UnixStream to synchronous: multiple references",
))?
.into_inner()?;
inner.set_nonblocking(false)?;
Ok(inner)
}
}
impl AsRawFd for UnixStream {
fn as_raw_fd(&self) -> RawFd {
self.watcher.as_raw_fd()

Loading…
Cancel
Save