https://github.com/smol-rs/async-dup
Duplicate an async I/O handle
https://github.com/smol-rs/async-dup
async rust
Last synced: about 1 year ago
JSON representation
Duplicate an async I/O handle
- Host: GitHub
- URL: https://github.com/smol-rs/async-dup
- Owner: smol-rs
- License: apache-2.0
- Created: 2020-05-31T14:56:13.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2025-01-19T15:52:48.000Z (over 1 year ago)
- Last Synced: 2025-04-04T02:08:03.787Z (over 1 year ago)
- Topics: async, rust
- Language: Rust
- Homepage:
- Size: 33.2 KB
- Stars: 40
- Watchers: 3
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# async-dup
[](
https://github.com/smol-rs/async-dup/actions)
[](
https://github.com/smol-rs/async-dup)
[](
https://crates.io/crates/async-dup)
[](
https://docs.rs/async-dup)
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:
```rust
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::::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:
```rust
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 {
let stream = Mutex::new(stream);
io::copy(&stream, &mut &stream).await
}
```
## License
Licensed under either of
* Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
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.