Go to file
Taiki Endo 9139c9c03b Update license badge to match Cargo.toml 2021-02-14 13:39:13 +09:00
.github/workflows Fix CI 2020-12-24 19:44:02 +09:00
src Update smol to 1 2020-12-26 22:16:18 +09:00
.gitignore Initial commit 2020-05-31 16:55:34 +02:00
CHANGELOG.md Bump to v1.2.2 2020-09-19 00:28:24 +02:00
Cargo.toml Update URLs 2020-12-26 23:47:13 +09:00
LICENSE-APACHE Initial commit 2020-05-31 16:55:34 +02:00
LICENSE-MIT Initial commit 2020-05-31 16:55:34 +02:00
README.md Update license badge to match Cargo.toml 2021-02-14 13:39:13 +09:00

README.md

async-dup

Build License Cargo Documentation

Duplicate an async I/O handle.

This crate provides two tools, Arc and Mutex:

  • Arc implements AsyncRead, AsyncWrite, and AsyncSeek if a reference to the inner type does.
  • A reference to Mutex implements AsyncRead, AsyncWrite, and AsyncSeek if 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
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.