mirror of
https://github.com/async-rs/async-std.git
synced 2025-01-30 17:25:32 +00:00
Add TryFrom
impls to convert async types to corresponding sync types
Add `TryFrom` implementations to convert `TcpListener`, `TcpStream`, `UdpSocket`, `UnixDatagram`, `UnixListener`, and `UnixStream` to their synchronous equivalents, including putting them back into blocking mode.
This commit is contained in:
parent
d9aaefb6c9
commit
1356551ba6
6 changed files with 70 additions and 0 deletions
|
@ -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! {
|
cfg_unix! {
|
||||||
use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
|
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! {
|
cfg_unix! {
|
||||||
use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
|
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! {
|
cfg_unix! {
|
||||||
use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
|
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 {
|
impl AsRawFd for UnixDatagram {
|
||||||
fn as_raw_fd(&self) -> RawFd {
|
fn as_raw_fd(&self) -> RawFd {
|
||||||
self.watcher.as_raw_fd()
|
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 {
|
impl AsRawFd for UnixListener {
|
||||||
fn as_raw_fd(&self) -> RawFd {
|
fn as_raw_fd(&self) -> RawFd {
|
||||||
self.watcher.as_raw_fd()
|
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 {
|
impl AsRawFd for UnixStream {
|
||||||
fn as_raw_fd(&self) -> RawFd {
|
fn as_raw_fd(&self) -> RawFd {
|
||||||
self.watcher.as_raw_fd()
|
self.watcher.as_raw_fd()
|
||||||
|
|
Loading…
Reference in a new issue