//! 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`.
usecrate::io::{AsyncRead, AsyncWrite, ReadBuf};
use std::fmt; use std::io; use std::pin::Pin; use std::sync::Arc; use std::sync::Mutex; use std::task::{Context, Poll};
cfg_io_util! { /// The readable half of a value returned from [`split`](split()). pubstruct ReadHalf<T> {
inner: Arc<Inner<T>>,
}
/// The writable half of a value returned from [`split`](split()). pubstruct WriteHalf<T> {
inner: Arc<Inner<T>>,
}
/// Splits a single value implementing `AsyncRead + AsyncWrite` into separate /// `AsyncRead` and `AsyncWrite` handles. /// /// To restore this read/write object from its `ReadHalf` and /// `WriteHalf` use [`unsplit`](ReadHalf::unsplit()). pubfn split<T>(stream: T) -> (ReadHalf<T>, WriteHalf<T>) where
T: AsyncRead + AsyncWrite,
{ let is_write_vectored = stream.is_write_vectored();
let inner = Arc::new(Inner {
stream: Mutex::new(stream),
is_write_vectored,
});
// safety: we do not move the stream. let stream = unsafe { Pin::new_unchecked(&mut *guard) };
f(stream)
}
}
impl<T> ReadHalf<T> { /// Checks if this `ReadHalf` and some `WriteHalf` were split from the same /// stream. pubfn is_pair_of(&self, other: &WriteHalf<T>) -> bool {
other.is_pair_of(self)
}
/// Reunites 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. /// This can be checked ahead of time by calling [`is_pair_of()`](Self::is_pair_of). #[track_caller] pubfn unsplit(self, wr: WriteHalf<T>) -> T where
T: Unpin,
{ ifself.is_pair_of(&wr) {
drop(wr);
let inner = Arc::try_unwrap(self.inner)
.ok()
.expect("`Arc::try_unwrap` failed");
impl<T> WriteHalf<T> { /// Checks if this `WriteHalf` and some `ReadHalf` were split from the same /// stream. pubfn is_pair_of(&self, other: &ReadHalf<T>) -> bool {
Arc::ptr_eq(&self.inner, &other.inner)
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.