net: flatten split mod (#1797)

The misc `split` types (`ReadHalf`, `WriteHalf`, `SendHalf`, `RecvHalf`)
are moved up a module and the `*::split` module is removed.
This commit is contained in:
Carl Lerche
2019-11-20 11:29:32 -08:00
committed by GitHub
parent d4fec2c5d6
commit 15dce2d11a
6 changed files with 37 additions and 33 deletions
+8 -7
View File
@@ -115,12 +115,13 @@ mod tcp {
} }
mod udp { mod udp {
use tokio::net::udp::{RecvHalf, SendHalf};
use tokio::net::UdpSocket;
use futures::{future, Sink, SinkExt, Stream, StreamExt}; use futures::{future, Sink, SinkExt, Stream, StreamExt};
use std::{error::Error, io, net::SocketAddr}; use std::error::Error;
use tokio::net::udp::{ use std::io;
split::{UdpSocketRecvHalf, UdpSocketSendHalf}, use std::net::SocketAddr;
UdpSocket,
};
pub async fn connect( pub async fn connect(
addr: &SocketAddr, addr: &SocketAddr,
@@ -146,7 +147,7 @@ mod udp {
async fn send( async fn send(
mut stdin: impl Stream<Item = Result<Vec<u8>, io::Error>> + Unpin, mut stdin: impl Stream<Item = Result<Vec<u8>, io::Error>> + Unpin,
writer: &mut UdpSocketSendHalf, writer: &mut SendHalf,
) -> Result<(), io::Error> { ) -> Result<(), io::Error> {
while let Some(item) = stdin.next().await { while let Some(item) = stdin.next().await {
let buf = item?; let buf = item?;
@@ -158,7 +159,7 @@ mod udp {
async fn recv( async fn recv(
mut stdout: impl Sink<Vec<u8>, Error = io::Error> + Unpin, mut stdout: impl Sink<Vec<u8>, Error = io::Error> + Unpin,
reader: &mut UdpSocketRecvHalf, reader: &mut RecvHalf,
) -> Result<(), io::Error> { ) -> Result<(), io::Error> {
loop { loop {
let mut buf = vec![0; 1024]; let mut buf = vec![0; 1024];
+5 -4
View File
@@ -16,12 +16,13 @@
//! [`TcpListener`]: struct.TcpListener.html //! [`TcpListener`]: struct.TcpListener.html
mod listener; mod listener;
pub use self::listener::TcpListener; pub use listener::TcpListener;
mod incoming; mod incoming;
pub use self::incoming::Incoming; pub use incoming::Incoming;
pub mod split; mod split;
pub use split::{ReadHalf, WriteHalf};
mod stream; mod stream;
pub use self::stream::TcpStream; pub use stream::TcpStream;
+3 -2
View File
@@ -8,6 +8,7 @@
//! [`UdpSocket`]: struct.UdpSocket //! [`UdpSocket`]: struct.UdpSocket
mod socket; mod socket;
pub mod split; pub use socket::UdpSocket;
pub use self::socket::UdpSocket; mod split;
pub use split::{RecvHalf, SendHalf, ReuniteError};
+2 -2
View File
@@ -1,6 +1,6 @@
use crate::future::poll_fn; use crate::future::poll_fn;
use crate::io::PollEvented; use crate::io::PollEvented;
use crate::net::udp::split::{split, UdpSocketRecvHalf, UdpSocketSendHalf}; use crate::net::udp::split::{split, RecvHalf, SendHalf};
use crate::net::ToSocketAddrs; use crate::net::ToSocketAddrs;
use std::convert::TryFrom; use std::convert::TryFrom;
@@ -67,7 +67,7 @@ impl UdpSocket {
/// ///
/// See the module level documenation of [`split`](super::split) for more /// See the module level documenation of [`split`](super::split) for more
/// details. /// details.
pub fn split(self) -> (UdpSocketRecvHalf, UdpSocketSendHalf) { pub fn split(self) -> (RecvHalf, SendHalf) {
split(self) split(self)
} }
+12 -12
View File
@@ -26,26 +26,26 @@ use std::sync::Arc;
/// Use [`send_to`](#method.send_to) or [`send`](#method.send) to send /// Use [`send_to`](#method.send_to) or [`send`](#method.send) to send
/// datagrams. /// datagrams.
#[derive(Debug)] #[derive(Debug)]
pub struct UdpSocketSendHalf(Arc<UdpSocket>); pub struct SendHalf(Arc<UdpSocket>);
/// The recv half after [`split`](super::UdpSocket::split). /// The recv half after [`split`](super::UdpSocket::split).
/// ///
/// Use [`recv_from`](#method.recv_from) or [`recv`](#method.recv) to receive /// Use [`recv_from`](#method.recv_from) or [`recv`](#method.recv) to receive
/// datagrams. /// datagrams.
#[derive(Debug)] #[derive(Debug)]
pub struct UdpSocketRecvHalf(Arc<UdpSocket>); pub struct RecvHalf(Arc<UdpSocket>);
pub(crate) fn split(socket: UdpSocket) -> (UdpSocketRecvHalf, UdpSocketSendHalf) { pub(crate) fn split(socket: UdpSocket) -> (RecvHalf, SendHalf) {
let shared = Arc::new(socket); let shared = Arc::new(socket);
let send = shared.clone(); let send = shared.clone();
let recv = shared; let recv = shared;
(UdpSocketRecvHalf(recv), UdpSocketSendHalf(send)) (RecvHalf(recv), SendHalf(send))
} }
/// Error indicating two halves were not from the same socket, and thus could /// Error indicating two halves were not from the same socket, and thus could
/// not be `reunite`d. /// not be `reunite`d.
#[derive(Debug)] #[derive(Debug)]
pub struct ReuniteError(pub UdpSocketSendHalf, pub UdpSocketRecvHalf); pub struct ReuniteError(pub SendHalf, pub RecvHalf);
impl fmt::Display for ReuniteError { impl fmt::Display for ReuniteError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -58,7 +58,7 @@ impl fmt::Display for ReuniteError {
impl Error for ReuniteError {} impl Error for ReuniteError {}
fn reunite(s: UdpSocketSendHalf, r: UdpSocketRecvHalf) -> Result<UdpSocket, ReuniteError> { fn reunite(s: SendHalf, r: RecvHalf) -> Result<UdpSocket, ReuniteError> {
if Arc::ptr_eq(&s.0, &r.0) { if Arc::ptr_eq(&s.0, &r.0) {
drop(r); drop(r);
// Only two instances of the `Arc` are ever created, one for the // Only two instances of the `Arc` are ever created, one for the
@@ -71,11 +71,11 @@ fn reunite(s: UdpSocketSendHalf, r: UdpSocketRecvHalf) -> Result<UdpSocket, Reun
} }
} }
impl UdpSocketRecvHalf { impl RecvHalf {
/// Attempts to put the two "halves" of a `UdpSocket` back together and /// Attempts to put the two "halves" of a `UdpSocket` back together and
/// recover the original socket. Succeeds only if the two "halves" /// recover the original socket. Succeeds only if the two "halves"
/// originated from the same call to `UdpSocket::split`. /// originated from the same call to `UdpSocket::split`.
pub fn reunite(self, other: UdpSocketSendHalf) -> Result<UdpSocket, ReuniteError> { pub fn reunite(self, other: SendHalf) -> Result<UdpSocket, ReuniteError> {
reunite(other, self) reunite(other, self)
} }
@@ -106,11 +106,11 @@ impl UdpSocketRecvHalf {
} }
} }
impl UdpSocketSendHalf { impl SendHalf {
/// Attempts to put the two "halves" of a `UdpSocket` back together and /// Attempts to put the two "halves" of a `UdpSocket` back together and
/// recover the original socket. Succeeds only if the two "halves" /// recover the original socket. Succeeds only if the two "halves"
/// originated from the same call to `UdpSocket::split`. /// originated from the same call to `UdpSocket::split`.
pub fn reunite(self, other: UdpSocketRecvHalf) -> Result<UdpSocket, ReuniteError> { pub fn reunite(self, other: RecvHalf) -> Result<UdpSocket, ReuniteError> {
reunite(self, other) reunite(self, other)
} }
@@ -135,13 +135,13 @@ impl UdpSocketSendHalf {
} }
} }
impl AsRef<UdpSocket> for UdpSocketSendHalf { impl AsRef<UdpSocket> for SendHalf {
fn as_ref(&self) -> &UdpSocket { fn as_ref(&self) -> &UdpSocket {
&self.0 &self.0
} }
} }
impl AsRef<UdpSocket> for UdpSocketRecvHalf { impl AsRef<UdpSocket> for RecvHalf {
fn as_ref(&self) -> &UdpSocket { fn as_ref(&self) -> &UdpSocket {
&self.0 &self.0
} }
+7 -6
View File
@@ -3,18 +3,19 @@
//! This crate provides APIs for using Unix Domain Sockets with Tokio. //! This crate provides APIs for using Unix Domain Sockets with Tokio.
mod datagram; mod datagram;
pub use self::datagram::UnixDatagram; pub use datagram::UnixDatagram;
mod incoming; mod incoming;
pub use self::incoming::Incoming; pub use incoming::Incoming;
mod listener; mod listener;
pub use self::listener::UnixListener; pub use listener::UnixListener;
pub mod split; mod split;
pub use split::{ReadHalf, WriteHalf};
mod stream; mod stream;
pub use self::stream::UnixStream; pub use stream::UnixStream;
mod ucred; mod ucred;
pub use self::ucred::UCred; pub use ucred::UCred;