https://github.com/banyc/async_async_io
`AsyncRead`, `AsyncWrite` traits but with `async fn` methods.
https://github.com/banyc/async_async_io
io tokio
Last synced: 8 months ago
JSON representation
`AsyncRead`, `AsyncWrite` traits but with `async fn` methods.
- Host: GitHub
- URL: https://github.com/banyc/async_async_io
- Owner: Banyc
- Created: 2023-06-13T11:19:10.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-11-01T12:41:11.000Z (about 1 year ago)
- Last Synced: 2025-05-03T06:49:03.588Z (9 months ago)
- Topics: io, tokio
- Language: Rust
- Homepage: https://crates.io/crates/async_async_io
- Size: 26.4 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# `async_async_io`
Currently, only for `tokio`.
## `impl_trait_in_assoc_type` (Optional)
- use nightly channel;
- enable the feature `"impl_trait_in_assoc_type"`;
- look into the tests in `src/read.rs` and `src/write.rs` to see how to implement the traits.
## Usage
### `AsyncAsyncRead`
Definition:
```rust
pub struct AsyncReadBytes {
reader: std::io::Cursor>,
}
impl async_async_io::read::AsyncAsyncRead for AsyncReadBytes {
async fn read(&mut self, buf: &mut [u8]) -> std::io::Result {
let len = std::io::Read::read(&mut self.reader, buf)?;
print!("{}.", len);
Ok(len)
}
}
```
Conversion to `AsyncRead`:
```rust
let stream = AsyncReadBytes::new(b"hello world".to_vec());
let mut async_read = async_async_io::read::PollRead::new(stream);
let mut writer = [0; 5];
async_read.read_exact(&mut writer).await.unwrap();
assert_eq!(&writer, b"hello");
```
### `AsyncAsyncWrite`
Definition:
```rust
pub struct AsyncWriteBytes {
writer: Vec,
}
impl async_async_io::write::AsyncAsyncWrite for AsyncWriteBytes {
async fn write(&mut self, buf: &[u8]) -> std::io::Result {
print!("{}.", buf.len());
std::io::Write::write(&mut self.writer, buf)
}
async fn flush(&mut self) -> std::io::Result<()> {
std::io::Write::flush(&mut self.writer)
}
async fn shutdown(&mut self) -> std::io::Result<()> {
Ok(())
}
}
```
Conversion to `AsyncWrite`:
```rust
let writer = AsyncWriteBytes::new();
let mut async_write = async_async_io::write::PollWrite::new(writer);
async_write.write_all(b"hello world").await.unwrap();
assert_eq!(async_write.into_inner().written(), b"hello world");
```