Struct DuplexStream

pub struct DuplexStream { /* private fields */ }

A bidirectional pipe to read and write bytes in memory.

A pair of DuplexStreams are created together, and they act as a "channel" that can be used as in-memory IO types. Writing to one of the pairs will allow that data to be read from the other, and vice versa.

Closing a DuplexStream

If one end of the DuplexStream channel is dropped, any pending reads on the other side will continue to read data until the buffer is drained, then they will signal EOF by returning 0 bytes. Any writes to the other side, including pending ones (that are waiting for free space in the buffer) will return Err(BrokenPipe) immediately.

Example

# async fn ex() -> std::io::Result<()> {
# use tokio::io::{AsyncReadExt, AsyncWriteExt};
let (mut client, mut server) = tokio::io::duplex(64);

client.write_all(b"ping").await?;

let mut buf = [0u8; 4];
server.read_exact(&mut buf).await?;
assert_eq!(&buf, b"ping");

server.write_all(b"pong").await?;

client.read_exact(&mut buf).await?;
assert_eq!(&buf, b"pong");
# Ok(())
# }

Trait Implementations

impl AsyncRead for DuplexStream

fn poll_read(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>) -> Poll<Result<()>>

impl AsyncWrite for DuplexStream

fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>>
fn poll_write_vectored(self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>]) -> Poll<Result<usize, Error>>
fn is_write_vectored(&self) -> bool
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

impl Debug for DuplexStream

fn fmt(&self, f: &mut Formatter<'_>) -> Result

impl Drop for DuplexStream

fn drop(&mut self)

Auto Trait Implementations

impl !RefUnwindSafe for DuplexStream

impl !UnwindSafe for DuplexStream

impl Freeze for DuplexStream

impl Send for DuplexStream

impl Sync for DuplexStream

impl Unpin for DuplexStream

impl UnsafeUnpin for DuplexStream

Blanket Implementations

impl<R> AsyncReadExt for DuplexStream where R: AsyncRead + ?Sized,

impl<T> Any for DuplexStream where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for DuplexStream where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for DuplexStream where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> From<T> for DuplexStream

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into<U> for DuplexStream where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for DuplexStream where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for DuplexStream where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

impl<W> AsyncWriteExt for DuplexStream where W: AsyncWrite + ?Sized,