7 releases (stable)

1.2.2 Sep 18, 2020
1.2.1 Jul 7, 2020
1.1.0 Jun 19, 2020
1.0.1 May 31, 2020
0.1.0 May 31, 2020

#426 in Asynchronous

Download history 858/week @ 2020-11-07 731/week @ 2020-11-14 534/week @ 2020-11-21 870/week @ 2020-11-28 1447/week @ 2020-12-05 1359/week @ 2020-12-12 1921/week @ 2020-12-19 1833/week @ 2020-12-26 3293/week @ 2021-01-02 4171/week @ 2021-01-09 5225/week @ 2021-01-16 6572/week @ 2021-01-23 6302/week @ 2021-01-30 6959/week @ 2021-02-06 8588/week @ 2021-02-13 6748/week @ 2021-02-20

2,810 downloads per month
Used in 56 crates (11 directly)

Apache-2.0 OR MIT

17KB
276 lines

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.

Dependencies

~68KB