Expand description
Duplicate an async I/O handle.
This crate provides two tools, Arc and Mutex:
ArcimplementsAsyncRead,AsyncWrite, andAsyncSeekif a reference to the inner type does.- A reference to
MuteximplementsAsyncRead,AsyncWrite, andAsyncSeekif the inner type does.
Wrap an async I/O handle in Arc or Mutex to clone it or share among tasks.
§Examples
Clone an async I/O handle:
use async_dup::Arc;
use futures::io;
use smol::Async;
use std::net::TcpStream;
// A client that echoes messages back to the server.
let stream = Async::<TcpStream>::connect(([127, 0, 0, 1], 8000)).await?;
// Create two handles to the stream.
let reader = Arc::new(stream);
let mut writer = reader.clone();
// Echo data received from the reader back into the writer.
io::copy(reader, &mut writer).await?;Share an async I/O handle:
use async_dup::Mutex;
use futures::io;
use futures::prelude::*;
// Reads data from a stream and echoes it back.
async fn echo(stream: impl AsyncRead + AsyncWrite + Unpin) -> io::Result<u64> {
let stream = Mutex::new(stream);
io::copy(&stream, &mut &stream).await
}Structs§
- Arc
- A reference-counted pointer that implements async I/O traits.
- Mutex
- A mutex that implements async I/O traits.
- Mutex
Guard - A guard that releases the mutex when dropped.