mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-16 00:00:12 +02:00
add TryFrom/From implementations (#1347)
* TryFrom<net::TcpListener> for TcpListener * TryFrom<net::TcpStream> for TcpStream * TryFrom<net::UdpSocket> for UdpSocket * TryFrom<net::UnixDatagram> for UnixDatagram * TryFrom<net::UnixListener> for UnixListener * TryFrom<net::UnixStream> for UnixStream * TryFrom<UnixDatagram> for mio_uds::UnixDatagram * TryFrom<File> for io::File * From<io::File> for File
This commit is contained in:
@@ -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<StdFile> for File {
|
||||
fn from(std: StdFile) -> Self {
|
||||
Self::from_std(std)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<File> for StdFile {
|
||||
type Error = io::Error;
|
||||
|
||||
fn try_from(mut file: File) -> Result<Self, Self::Error> {
|
||||
file.std
|
||||
.take()
|
||||
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "`File` instance already shutdown"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -351,6 +351,18 @@ impl TryFrom<TcpListener> for mio::net::TcpListener {
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<net::TcpListener> 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, Self::Error> {
|
||||
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)
|
||||
|
||||
@@ -886,6 +886,18 @@ impl TryFrom<TcpStream> for mio::net::TcpStream {
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<net::TcpStream> 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, Self::Error> {
|
||||
Self::from_std(stream, &Handle::default())
|
||||
}
|
||||
}
|
||||
|
||||
// ===== impl Read / Write =====
|
||||
|
||||
impl AsyncRead for TcpStream {
|
||||
|
||||
@@ -451,6 +451,18 @@ impl TryFrom<UdpSocket> for mio::net::UdpSocket {
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<net::UdpSocket> 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, Self::Error> {
|
||||
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)
|
||||
|
||||
@@ -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<UnixDatagram> 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<Self, Self::Error> {
|
||||
value.io.into_inner()
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<net::UnixDatagram> 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, Self::Error> {
|
||||
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)
|
||||
|
||||
@@ -155,6 +155,18 @@ impl TryFrom<UnixListener> for mio_uds::UnixListener {
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<net::UnixListener> 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> {
|
||||
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)
|
||||
|
||||
@@ -130,6 +130,18 @@ impl TryFrom<UnixStream> for mio_uds::UnixStream {
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<net::UnixStream> 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> {
|
||||
Self::from_std(stream, &Handle::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl AsyncRead for UnixStream {
|
||||
unsafe fn prepare_uninitialized_buffer(&self, _: &mut [u8]) -> bool {
|
||||
false
|
||||
|
||||
Reference in New Issue
Block a user