8 releases (stable)

1.2.4 Dec 2, 2023
1.2.3 Sep 25, 2023
1.2.2 Sep 18, 2020
1.2.1 Jul 7, 2020
0.1.0 May 31, 2020

#1970 in Asynchronous

Download history 25289/week @ 2024-08-01 26510/week @ 2024-08-08 22558/week @ 2024-08-15 22610/week @ 2024-08-22 20763/week @ 2024-08-29 24502/week @ 2024-09-05 27291/week @ 2024-09-12 26377/week @ 2024-09-19 27928/week @ 2024-09-26 29590/week @ 2024-10-03 25078/week @ 2024-10-10 30246/week @ 2024-10-17 25440/week @ 2024-10-24 25867/week @ 2024-10-31 22404/week @ 2024-11-07 18988/week @ 2024-11-14

99,516 downloads per month
Used in 209 crates (32 directly)

Apache-2.0 OR MIT

18KB
296 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

~540KB