mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-17 00:00:11 +02:00
io: bring back split utility (#1521)
Bring back `split` utility as a free fn instead of a method on `AsyncRead`. This utility wraps the `stream` in an `Arc` and uses mutual exclusion to ensure correct access. Additionally, the specialized `split_mut` fn on TcpStream and UdsStream is promoted to `split`.
This commit is contained in:
@@ -22,6 +22,9 @@ mod async_write;
|
||||
#[cfg(feature = "util")]
|
||||
mod io;
|
||||
|
||||
#[cfg(feature = "util")]
|
||||
pub mod split;
|
||||
|
||||
pub use self::async_buf_read::AsyncBufRead;
|
||||
pub use self::async_read::AsyncRead;
|
||||
pub use self::async_write::AsyncWrite;
|
||||
|
||||
@@ -0,0 +1,173 @@
|
||||
//! Split a single value implementing `AsyncRead + AsyncWrite` into separate
|
||||
//! `AsyncRead` and `AsyncWrite` handles.
|
||||
//!
|
||||
//! To restore this read/write object from its `split::ReadHalf` and
|
||||
//! `split::WriteHalf` use `unsplit`.
|
||||
|
||||
use crate::{AsyncRead, AsyncWrite};
|
||||
|
||||
use bytes::{Buf, BufMut};
|
||||
use futures_core::ready;
|
||||
use std::cell::UnsafeCell;
|
||||
use std::fmt;
|
||||
use std::io;
|
||||
use std::pin::Pin;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::atomic::Ordering::{Acquire, Release};
|
||||
use std::sync::Arc;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
/// The readable half of a value returned from `split`.
|
||||
pub struct ReadHalf<T> {
|
||||
inner: Arc<Inner<T>>,
|
||||
}
|
||||
|
||||
/// The writable half of a value returned from `split`.
|
||||
pub struct WriteHalf<T> {
|
||||
inner: Arc<Inner<T>>,
|
||||
}
|
||||
|
||||
struct Inner<T> {
|
||||
locked: AtomicBool,
|
||||
stream: UnsafeCell<T>,
|
||||
}
|
||||
|
||||
struct Guard<'a, T> {
|
||||
inner: &'a Inner<T>,
|
||||
}
|
||||
|
||||
/// Split a single value implementing `AsyncRead + AsyncWrite` into separate
|
||||
/// `AsyncRead` and `AsyncWrite` handles.
|
||||
///
|
||||
/// To restore this read/write object from its `split::ReadHalf` and
|
||||
/// `split::WriteHalf` use `unsplit`.
|
||||
pub fn split<T>(stream: T) -> (ReadHalf<T>, WriteHalf<T>)
|
||||
where
|
||||
T: AsyncRead + AsyncWrite,
|
||||
{
|
||||
let inner = Arc::new(Inner {
|
||||
locked: AtomicBool::new(false),
|
||||
stream: UnsafeCell::new(stream),
|
||||
});
|
||||
|
||||
let rd = ReadHalf {
|
||||
inner: inner.clone(),
|
||||
};
|
||||
|
||||
let wr = WriteHalf { inner };
|
||||
|
||||
(rd, wr)
|
||||
}
|
||||
|
||||
impl<T> ReadHalf<T> {
|
||||
/// Reunite with a previously split `WriteHalf`.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// If this `ReadHalf` and the given `WriteHalf` do not originate from the
|
||||
/// same `split` operation this method will panic.
|
||||
pub fn unsplit(self, wr: WriteHalf<T>) -> T {
|
||||
if Arc::ptr_eq(&self.inner, &wr.inner) {
|
||||
drop(wr);
|
||||
|
||||
let inner = Arc::try_unwrap(self.inner)
|
||||
.ok()
|
||||
.expect("Arc::try_unwrap failed");
|
||||
|
||||
inner.stream.into_inner()
|
||||
} else {
|
||||
panic!("Unrelated `split::Write` passed to `split::Read::unsplit`.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: AsyncRead> AsyncRead for ReadHalf<T> {
|
||||
fn poll_read(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &mut [u8],
|
||||
) -> Poll<io::Result<usize>> {
|
||||
let mut inner = ready!(self.inner.poll_lock(cx));
|
||||
inner.stream_pin().poll_read(cx, buf)
|
||||
}
|
||||
|
||||
fn poll_read_buf<B: BufMut>(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &mut B,
|
||||
) -> Poll<io::Result<usize>> {
|
||||
let mut inner = ready!(self.inner.poll_lock(cx));
|
||||
inner.stream_pin().poll_read_buf(cx, buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: AsyncWrite> AsyncWrite for WriteHalf<T> {
|
||||
fn poll_write(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &[u8],
|
||||
) -> Poll<Result<usize, io::Error>> {
|
||||
let mut inner = ready!(self.inner.poll_lock(cx));
|
||||
inner.stream_pin().poll_write(cx, buf)
|
||||
}
|
||||
|
||||
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
|
||||
let mut inner = ready!(self.inner.poll_lock(cx));
|
||||
inner.stream_pin().poll_flush(cx)
|
||||
}
|
||||
|
||||
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
|
||||
let mut inner = ready!(self.inner.poll_lock(cx));
|
||||
inner.stream_pin().poll_shutdown(cx)
|
||||
}
|
||||
|
||||
fn poll_write_buf<B: Buf>(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &mut B,
|
||||
) -> Poll<Result<usize, io::Error>> {
|
||||
let mut inner = ready!(self.inner.poll_lock(cx));
|
||||
inner.stream_pin().poll_write_buf(cx, buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Inner<T> {
|
||||
fn poll_lock(&self, cx: &mut Context<'_>) -> Poll<Guard<'_, T>> {
|
||||
if !self.locked.compare_and_swap(false, true, Acquire) {
|
||||
Poll::Ready(Guard { inner: self })
|
||||
} else {
|
||||
// Spin... but investigate a better strategy
|
||||
|
||||
::std::thread::yield_now();
|
||||
cx.waker().wake_by_ref();
|
||||
|
||||
Poll::Pending
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Guard<'_, T> {
|
||||
fn stream_pin(&mut self) -> Pin<&mut T> {
|
||||
// safety: the stream is pinned in `Arc` and the `Guard` ensures mutual
|
||||
// exclusion.
|
||||
unsafe { Pin::new_unchecked(&mut *self.inner.stream.get()) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Drop for Guard<'_, T> {
|
||||
fn drop(&mut self) {
|
||||
self.inner.locked.store(false, Release);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: fmt::Debug> fmt::Debug for ReadHalf<T> {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
fmt.debug_struct("split::ReadHalf").finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: fmt::Debug> fmt::Debug for WriteHalf<T> {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
fmt.debug_struct("split::WriteHalf").finish()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
use tokio::io::{split, AsyncRead, AsyncWrite};
|
||||
|
||||
use std::io;
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
struct RW;
|
||||
|
||||
impl AsyncRead for RW {
|
||||
fn poll_read(
|
||||
self: Pin<&mut Self>,
|
||||
_cx: &mut Context<'_>,
|
||||
_buf: &mut [u8],
|
||||
) -> Poll<io::Result<usize>> {
|
||||
Poll::Ready(Ok(1))
|
||||
}
|
||||
}
|
||||
|
||||
impl AsyncWrite for RW {
|
||||
fn poll_write(
|
||||
self: Pin<&mut Self>,
|
||||
_cx: &mut Context<'_>,
|
||||
_buf: &[u8],
|
||||
) -> Poll<Result<usize, io::Error>> {
|
||||
Poll::Ready(Ok(1))
|
||||
}
|
||||
|
||||
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
|
||||
Poll::Ready(Ok(()))
|
||||
}
|
||||
|
||||
fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
|
||||
Poll::Ready(Ok(()))
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsplit_ok() {
|
||||
let (r, w) = split(RW);
|
||||
r.unsplit(w);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn unsplit_err1() {
|
||||
let (r, _) = split(RW);
|
||||
let (_, w) = split(RW);
|
||||
r.unsplit(w);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn unsplit_err2() {
|
||||
let (_, w) = split(RW);
|
||||
let (r, _) = split(RW);
|
||||
r.unsplit(w);
|
||||
}
|
||||
+17
-154
@@ -1,137 +1,39 @@
|
||||
//! `TcpStream` split support.
|
||||
//!
|
||||
//! A `TcpStream` can be split into a `TcpStreamReadHalf` and a
|
||||
//! `TcpStreamWriteHalf` with the `TcpStream::split` method. `TcpStreamReadHalf`
|
||||
//! implements `AsyncRead` while `TcpStreamWriteHalf` implements `AsyncWrite`.
|
||||
//! The two halves can be used concurrently, even from multiple tasks.
|
||||
//! A `TcpStream` can be split into a `ReadHalf` and a
|
||||
//! `WriteHalf` with the `TcpStream::split` method. `ReadHalf`
|
||||
//! implements `AsyncRead` while `WriteHalf` implements `AsyncWrite`.
|
||||
//!
|
||||
//! Compared to the generic split of `AsyncRead + AsyncWrite`, this specialized
|
||||
//! split gives read and write halves that are faster and smaller, because they
|
||||
//! do not use locks. They also provide access to the underlying `TcpStream`
|
||||
//! after split, implementing `AsRef<TcpStream>`. This allows you to call
|
||||
//! `TcpStream` methods that takes `&self`, e.g., to get local and peer
|
||||
//! addresses, to get and set socket options, and to shutdown the sockets.
|
||||
//! split has no associated overhead and enforces all invariants at the type
|
||||
//! level.
|
||||
|
||||
use super::TcpStream;
|
||||
|
||||
use tokio_io::{AsyncRead, AsyncWrite};
|
||||
|
||||
use bytes::{Buf, BufMut};
|
||||
use std::error::Error;
|
||||
use std::fmt;
|
||||
use std::io;
|
||||
use std::net::Shutdown;
|
||||
use std::pin::Pin;
|
||||
use std::sync::Arc;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
/// Read half of a `TcpStream`.
|
||||
#[derive(Debug)]
|
||||
pub struct TcpStreamReadHalf(Arc<TcpStream>);
|
||||
pub struct ReadHalf<'a>(&'a TcpStream);
|
||||
|
||||
/// Write half of a `TcpStream`.
|
||||
///
|
||||
/// Note that in the `AsyncWrite` implemenation of `TcpStreamWriteHalf`,
|
||||
/// `poll_shutdown` actually shuts down the TCP stream in the write direction.
|
||||
#[derive(Debug)]
|
||||
pub struct TcpStreamWriteHalf(Arc<TcpStream>);
|
||||
pub struct WriteHalf<'a>(&'a TcpStream);
|
||||
|
||||
pub(crate) fn split(stream: TcpStream) -> (TcpStreamReadHalf, TcpStreamWriteHalf) {
|
||||
let shared = Arc::new(stream);
|
||||
(
|
||||
TcpStreamReadHalf(shared.clone()),
|
||||
TcpStreamWriteHalf(shared),
|
||||
)
|
||||
pub(crate) fn split(stream: &mut TcpStream) -> (ReadHalf<'_>, WriteHalf<'_>) {
|
||||
(ReadHalf(&*stream), WriteHalf(&*stream))
|
||||
}
|
||||
|
||||
/// Read half of a `TcpStream`.
|
||||
#[derive(Debug)]
|
||||
pub struct TcpStreamReadHalfMut<'a>(&'a TcpStream);
|
||||
|
||||
/// Write half of a `TcpStream`.
|
||||
///
|
||||
/// Note that in the `AsyncWrite` implemenation of `TcpStreamWriteHalf`,
|
||||
/// `poll_shutdown` actually shuts down the TCP stream in the write direction.
|
||||
#[derive(Debug)]
|
||||
pub struct TcpStreamWriteHalfMut<'a>(&'a TcpStream);
|
||||
|
||||
pub(crate) fn split_mut(
|
||||
stream: &mut TcpStream,
|
||||
) -> (TcpStreamReadHalfMut<'_>, TcpStreamWriteHalfMut<'_>) {
|
||||
(
|
||||
TcpStreamReadHalfMut(&*stream),
|
||||
TcpStreamWriteHalfMut(&*stream),
|
||||
)
|
||||
}
|
||||
|
||||
/// Error indicating two halves were not from the same stream, and thus could
|
||||
/// not be `reunite`d.
|
||||
#[derive(Debug)]
|
||||
pub struct ReuniteError(pub TcpStreamReadHalf, pub TcpStreamWriteHalf);
|
||||
|
||||
impl fmt::Display for ReuniteError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"tried to reunite halves that are not from the same stream"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for ReuniteError {}
|
||||
|
||||
impl TcpStreamReadHalf {
|
||||
/// Attempts to put the two "halves" of a `TcpStream` back together and
|
||||
/// recover the original stream. Succeeds only if the two "halves"
|
||||
/// originated from the same call to `TcpStream::split`.
|
||||
pub fn reunite(self, other: TcpStreamWriteHalf) -> Result<TcpStream, ReuniteError> {
|
||||
if Arc::ptr_eq(&self.0, &other.0) {
|
||||
drop(other);
|
||||
// Only two instances of the `Arc` are ever created, one for the
|
||||
// reader and one for the writer, and those `Arc`s are never exposed
|
||||
// externally. And so when we drop one here, the other one must be
|
||||
// the only remaining one.
|
||||
Ok(Arc::try_unwrap(self.0).expect("tcp: try_unwrap failed in reunite"))
|
||||
} else {
|
||||
Err(ReuniteError(self, other))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TcpStreamWriteHalf {
|
||||
/// Attempts to put the two "halves" of a `TcpStream` back together and
|
||||
/// recover the original stream. Succeeds only if the two "halves"
|
||||
/// originated from the same call to `TcpStream::split`.
|
||||
pub fn reunite(self, other: TcpStreamReadHalf) -> Result<TcpStream, ReuniteError> {
|
||||
other.reunite(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<TcpStream> for TcpStreamReadHalf {
|
||||
fn as_ref(&self) -> &TcpStream {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<TcpStream> for TcpStreamWriteHalf {
|
||||
fn as_ref(&self) -> &TcpStream {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<TcpStream> for TcpStreamReadHalfMut<'_> {
|
||||
fn as_ref(&self) -> &TcpStream {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<TcpStream> for TcpStreamWriteHalfMut<'_> {
|
||||
fn as_ref(&self) -> &TcpStream {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl AsyncRead for TcpStreamReadHalf {
|
||||
impl AsyncRead for ReadHalf<'_> {
|
||||
unsafe fn prepare_uninitialized_buffer(&self, _: &mut [u8]) -> bool {
|
||||
false
|
||||
}
|
||||
@@ -153,7 +55,7 @@ impl AsyncRead for TcpStreamReadHalf {
|
||||
}
|
||||
}
|
||||
|
||||
impl AsyncWrite for TcpStreamWriteHalf {
|
||||
impl AsyncWrite for WriteHalf<'_> {
|
||||
fn poll_write(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
@@ -182,53 +84,14 @@ impl AsyncWrite for TcpStreamWriteHalf {
|
||||
}
|
||||
}
|
||||
|
||||
impl AsyncRead for TcpStreamReadHalfMut<'_> {
|
||||
unsafe fn prepare_uninitialized_buffer(&self, _: &mut [u8]) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn poll_read(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &mut [u8],
|
||||
) -> Poll<io::Result<usize>> {
|
||||
self.0.poll_read_priv(cx, buf)
|
||||
}
|
||||
|
||||
fn poll_read_buf<B: BufMut>(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &mut B,
|
||||
) -> Poll<io::Result<usize>> {
|
||||
self.0.poll_read_buf_priv(cx, buf)
|
||||
impl AsRef<TcpStream> for ReadHalf<'_> {
|
||||
fn as_ref(&self) -> &TcpStream {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl AsyncWrite for TcpStreamWriteHalfMut<'_> {
|
||||
fn poll_write(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &[u8],
|
||||
) -> Poll<io::Result<usize>> {
|
||||
self.0.poll_write_priv(cx, buf)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> {
|
||||
// tcp flush is a no-op
|
||||
Poll::Ready(Ok(()))
|
||||
}
|
||||
|
||||
// `poll_shutdown` on a write half shutdowns the stream in the "write" direction.
|
||||
fn poll_shutdown(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> {
|
||||
self.0.shutdown(Shutdown::Write).into()
|
||||
}
|
||||
|
||||
fn poll_write_buf<B: Buf>(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &mut B,
|
||||
) -> Poll<io::Result<usize>> {
|
||||
self.0.poll_write_buf_priv(cx, buf)
|
||||
impl AsRef<TcpStream> for WriteHalf<'_> {
|
||||
fn as_ref(&self) -> &TcpStream {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
use super::split::{
|
||||
split, split_mut, TcpStreamReadHalf, TcpStreamReadHalfMut, TcpStreamWriteHalf,
|
||||
TcpStreamWriteHalfMut,
|
||||
};
|
||||
use super::split::{split, ReadHalf, WriteHalf};
|
||||
use crate::driver::Handle;
|
||||
use crate::util::PollEvented;
|
||||
use crate::ToSocketAddrs;
|
||||
@@ -584,19 +581,10 @@ impl TcpStream {
|
||||
///
|
||||
/// See the module level documenation of [`split`](super::split) for more
|
||||
/// details.
|
||||
pub fn split(self) -> (TcpStreamReadHalf, TcpStreamWriteHalf) {
|
||||
pub fn split(&mut self) -> (ReadHalf<'_>, WriteHalf<'_>) {
|
||||
split(self)
|
||||
}
|
||||
|
||||
/// Split a `TcpStream` into a read half and a write half, which can be used
|
||||
/// to read and write the stream concurrently.
|
||||
///
|
||||
/// See the module level documenation of [`split`](super::split) for more
|
||||
/// details.
|
||||
pub fn split_mut(&mut self) -> (TcpStreamReadHalfMut<'_>, TcpStreamWriteHalfMut<'_>) {
|
||||
split_mut(self)
|
||||
}
|
||||
|
||||
// == Poll IO functions that takes `&self` ==
|
||||
//
|
||||
// They are not public because (taken from the doc of `PollEvented`):
|
||||
|
||||
+20
-111
@@ -1,15 +1,12 @@
|
||||
//! `UnixStream` split support.
|
||||
//!
|
||||
//! A `UnixStream` can be split into a read half and a write half with `UnixStream::split`
|
||||
//! and `UnixStream::split_mut` methods. The read half implements `AsyncRead` while
|
||||
//! the write half implements `AsyncWrite`. The two halves can be used concurrently.
|
||||
//! A `UnixStream` can be split into a read half and a write half with
|
||||
//! `UnixStream::split`. The read half implements `AsyncRead` while the write
|
||||
//! half implements `AsyncWrite`.
|
||||
//!
|
||||
//! Compared to the generic split of `AsyncRead + AsyncWrite`, this specialized
|
||||
//! split gives read and write halves that are faster and smaller, because they
|
||||
//! do not use locks. They also provide access to the underlying `UnixStream`
|
||||
//! after split, implementing `AsRef<UnixStream>`. This allows you to call
|
||||
//! `UnixStream` methods that takes `&self`, e.g., to get local and peer
|
||||
//! addresses, to get and set socket options, and to shutdown the sockets.
|
||||
//! split has no associated overhead and enforces all invariants at the type
|
||||
//! level.
|
||||
|
||||
use super::UnixStream;
|
||||
|
||||
@@ -19,73 +16,21 @@ use bytes::{Buf, BufMut};
|
||||
use std::io;
|
||||
use std::net::Shutdown;
|
||||
use std::pin::Pin;
|
||||
use std::sync::Arc;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
/// Read half of a `UnixStream`.
|
||||
#[derive(Debug)]
|
||||
pub struct UnixStreamReadHalf(Arc<UnixStream>);
|
||||
pub struct ReadHalf<'a>(&'a UnixStream);
|
||||
|
||||
/// Write half of a `UnixStream`.
|
||||
///
|
||||
/// Note that in the `AsyncWrite` implementation of `UnixStreamWriteHalf`,
|
||||
/// `poll_shutdown` actually shuts down the stream in the write direction.
|
||||
#[derive(Debug)]
|
||||
pub struct UnixStreamWriteHalf(Arc<UnixStream>);
|
||||
pub struct WriteHalf<'a>(&'a UnixStream);
|
||||
|
||||
/// Read half of a `UnixStream`.
|
||||
#[derive(Debug)]
|
||||
pub struct UnixStreamReadHalfMut<'a>(&'a UnixStream);
|
||||
|
||||
/// Write half of a `UnixStream`.
|
||||
///
|
||||
/// Note that in the `AsyncWrite` implementation of `UnixStreamWriteHalfMut`,
|
||||
/// `poll_shutdown` actually shuts down the stream in the write direction.
|
||||
#[derive(Debug)]
|
||||
pub struct UnixStreamWriteHalfMut<'a>(&'a UnixStream);
|
||||
|
||||
pub(crate) fn split(stream: UnixStream) -> (UnixStreamReadHalf, UnixStreamWriteHalf) {
|
||||
let shared = Arc::new(stream);
|
||||
(
|
||||
UnixStreamReadHalf(shared.clone()),
|
||||
UnixStreamWriteHalf(shared),
|
||||
)
|
||||
pub(crate) fn split(stream: &mut UnixStream) -> (ReadHalf<'_>, WriteHalf<'_>) {
|
||||
(ReadHalf(stream), WriteHalf(stream))
|
||||
}
|
||||
|
||||
pub(crate) fn split_mut(
|
||||
stream: &mut UnixStream,
|
||||
) -> (UnixStreamReadHalfMut<'_>, UnixStreamWriteHalfMut<'_>) {
|
||||
(
|
||||
UnixStreamReadHalfMut(stream),
|
||||
UnixStreamWriteHalfMut(stream),
|
||||
)
|
||||
}
|
||||
|
||||
impl AsRef<UnixStream> for UnixStreamReadHalf {
|
||||
fn as_ref(&self) -> &UnixStream {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<UnixStream> for UnixStreamWriteHalf {
|
||||
fn as_ref(&self) -> &UnixStream {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<UnixStream> for UnixStreamReadHalfMut<'_> {
|
||||
fn as_ref(&self) -> &UnixStream {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<UnixStream> for UnixStreamWriteHalfMut<'_> {
|
||||
fn as_ref(&self) -> &UnixStream {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl AsyncRead for UnixStreamReadHalf {
|
||||
impl AsyncRead for ReadHalf<'_> {
|
||||
unsafe fn prepare_uninitialized_buffer(&self, _: &mut [u8]) -> bool {
|
||||
false
|
||||
}
|
||||
@@ -107,29 +52,7 @@ impl AsyncRead for UnixStreamReadHalf {
|
||||
}
|
||||
}
|
||||
|
||||
impl AsyncRead for UnixStreamReadHalfMut<'_> {
|
||||
unsafe fn prepare_uninitialized_buffer(&self, _: &mut [u8]) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn poll_read(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &mut [u8],
|
||||
) -> Poll<io::Result<usize>> {
|
||||
self.0.poll_read_priv(cx, buf)
|
||||
}
|
||||
|
||||
fn poll_read_buf<B: BufMut>(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &mut B,
|
||||
) -> Poll<io::Result<usize>> {
|
||||
self.0.poll_read_buf_priv(cx, buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsyncWrite for UnixStreamWriteHalf {
|
||||
impl AsyncWrite for WriteHalf<'_> {
|
||||
fn poll_write(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
@@ -155,28 +78,14 @@ impl AsyncWrite for UnixStreamWriteHalf {
|
||||
}
|
||||
}
|
||||
|
||||
impl AsyncWrite for UnixStreamWriteHalfMut<'_> {
|
||||
fn poll_write(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &[u8],
|
||||
) -> Poll<io::Result<usize>> {
|
||||
self.0.poll_write_priv(cx, buf)
|
||||
}
|
||||
|
||||
fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> {
|
||||
Poll::Ready(Ok(()))
|
||||
}
|
||||
|
||||
fn poll_shutdown(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> {
|
||||
self.0.shutdown(Shutdown::Write).into()
|
||||
}
|
||||
|
||||
fn poll_write_buf<B: Buf>(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &mut B,
|
||||
) -> Poll<io::Result<usize>> {
|
||||
self.0.poll_write_buf_priv(cx, buf)
|
||||
impl AsRef<UnixStream> for ReadHalf<'_> {
|
||||
fn as_ref(&self) -> &UnixStream {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<UnixStream> for WriteHalf<'_> {
|
||||
fn as_ref(&self) -> &UnixStream {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
use super::split::{
|
||||
split, split_mut, UnixStreamReadHalf, UnixStreamReadHalfMut, UnixStreamWriteHalf,
|
||||
UnixStreamWriteHalfMut,
|
||||
};
|
||||
use super::split::{split, ReadHalf, WriteHalf};
|
||||
use super::ucred::{self, UCred};
|
||||
use crate::driver::Handle;
|
||||
use crate::util::PollEvented;
|
||||
@@ -112,18 +109,9 @@ impl UnixStream {
|
||||
///
|
||||
/// See the module level documenation of [`split`](super::split) for more
|
||||
/// details.
|
||||
pub fn split(self) -> (UnixStreamReadHalf, UnixStreamWriteHalf) {
|
||||
pub fn split(&mut self) -> (ReadHalf<'_>, WriteHalf<'_>) {
|
||||
split(self)
|
||||
}
|
||||
|
||||
/// Split a `UnixStream` into a read half and a write half, which can be used
|
||||
/// to read and write the stream concurrently.
|
||||
///
|
||||
/// See the module level documenation of [`split`](super::split) for more
|
||||
/// details.
|
||||
pub fn split_mut(&mut self) -> (UnixStreamReadHalfMut<'_>, UnixStreamWriteHalfMut<'_>) {
|
||||
split_mut(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<UnixStream> for mio_uds::UnixStream {
|
||||
|
||||
@@ -31,7 +31,7 @@ async fn echo_server() {
|
||||
assert_ok!(tx.send(()));
|
||||
});
|
||||
|
||||
let (stream, _) = assert_ok!(srv.accept().await);
|
||||
let (mut stream, _) = assert_ok!(srv.accept().await);
|
||||
let (mut rd, mut wr) = stream.split();
|
||||
|
||||
let n = assert_ok!(rd.copy(&mut wr).await);
|
||||
|
||||
@@ -20,7 +20,7 @@ async fn shutdown() {
|
||||
assert_eq!(n, 0);
|
||||
});
|
||||
|
||||
let (stream, _) = assert_ok!(srv.accept().await);
|
||||
let (mut stream, _) = assert_ok!(srv.accept().await);
|
||||
let (mut rd, mut wr) = stream.split();
|
||||
|
||||
let n = assert_ok!(rd.copy(&mut wr).await);
|
||||
|
||||
@@ -1,25 +1 @@
|
||||
use tokio_net::tcp::{TcpListener, TcpStream};
|
||||
|
||||
#[tokio::test]
|
||||
async fn split_reunite() -> std::io::Result<()> {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await?;
|
||||
let addr = listener.local_addr()?;
|
||||
let stream = TcpStream::connect(&addr).await?;
|
||||
|
||||
let (r, w) = stream.split();
|
||||
assert!(r.reunite(w).is_ok());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn split_reunite_error() -> std::io::Result<()> {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await?;
|
||||
let addr = listener.local_addr()?;
|
||||
let stream = TcpStream::connect(&addr).await?;
|
||||
let stream1 = TcpStream::connect(&addr).await?;
|
||||
|
||||
let (r, _) = stream.split();
|
||||
let (_, w) = stream1.split();
|
||||
assert!(r.reunite(w).is_err());
|
||||
Ok(())
|
||||
}
|
||||
// TODO: write tests using TcpStream::split()
|
||||
|
||||
@@ -11,10 +11,10 @@ use tokio::prelude::*;
|
||||
/// writing by reading to the end of stream on the other side of the connection.
|
||||
#[tokio::test]
|
||||
async fn split() -> std::io::Result<()> {
|
||||
let (a, mut b) = UnixStream::pair()?;
|
||||
let (mut a, mut b) = UnixStream::pair()?;
|
||||
|
||||
let (mut a_read, mut a_write) = a.split();
|
||||
let (mut b_read, mut b_write) = b.split_mut();
|
||||
let (mut b_read, mut b_write) = b.split();
|
||||
|
||||
let (a_response, b_response) = futures::future::try_join(
|
||||
send_recv_all(&mut a_read, &mut a_write, b"A"),
|
||||
|
||||
@@ -93,7 +93,8 @@ mod tcp {
|
||||
stdin: impl Stream<Item = Result<Vec<u8>, io::Error>> + Unpin,
|
||||
mut stdout: impl Sink<Vec<u8>, Error = io::Error> + Unpin,
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
let (r, w) = TcpStream::connect(addr).await?.split();
|
||||
let mut stream = TcpStream::connect(addr).await?;
|
||||
let (r, w) = stream.split();
|
||||
let sink = FramedWrite::new(w, codec::Bytes);
|
||||
let mut stream = FramedRead::new(r, codec::Bytes).filter_map(|i| match i {
|
||||
Ok(i) => future::ready(Some(i)),
|
||||
|
||||
@@ -52,8 +52,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn transfer(inbound: TcpStream, proxy_addr: String) -> Result<(), Box<dyn Error>> {
|
||||
let outbound = TcpStream::connect(proxy_addr).await?;
|
||||
async fn transfer(mut inbound: TcpStream, proxy_addr: String) -> Result<(), Box<dyn Error>> {
|
||||
let mut outbound = TcpStream::connect(proxy_addr).await?;
|
||||
|
||||
let (mut ri, mut wi) = inbound.split();
|
||||
let (mut ro, mut wo) = outbound.split();
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
// standard input, output, and error
|
||||
#[cfg(feature = "fs")]
|
||||
pub use tokio_fs::{stderr, stdin, stdout, Stderr, Stdin, Stdout};
|
||||
pub use tokio_io::split::split;
|
||||
pub use tokio_io::{
|
||||
AsyncBufRead, AsyncBufReadExt, AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, BufReader,
|
||||
BufWriter,
|
||||
|
||||
Reference in New Issue
Block a user