| Crates.io | async_async_io |
| lib.rs | async_async_io |
| version | 0.2.3 |
| created_at | 2023-06-13 11:30:28.981185+00 |
| updated_at | 2024-11-01 12:18:29.524421+00 |
| description | `AsyncRead`, `AsyncWrite` traits but with `async fn` methods. |
| homepage | |
| repository | https://github.com/Banyc/async_async_io |
| max_upload_size | |
| id | 888939 |
| size | 22,275 |
async_async_ioCurrently, only for tokio.
impl_trait_in_assoc_type (Optional)"impl_trait_in_assoc_type";src/read.rs and src/write.rs to see how to implement the traits.AsyncAsyncReadDefinition:
pub struct AsyncReadBytes {
reader: io::Cursor<Vec<u8>>,
}
impl AsyncAsyncRead for AsyncReadBytes {
async fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let len = std::io::Read::read(&mut self.reader, buf)?;
print!("{}.", len);
Ok(len)
}
}
Conversion to AsyncRead:
let stream = AsyncReadBytes::new(b"hello world".to_vec());
let mut async_read = PollRead::new(stream);
let mut writer = [0; 5];
async_read.read_exact(&mut writer).await.unwrap();
assert_eq!(&writer, b"hello");
AsyncAsyncWriteDefinition:
pub struct AsyncWriteBytes {
writer: Vec<u8>,
}
impl AsyncAsyncWrite for AsyncWriteBytes {
async fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
print!("{}.", buf.len());
std::io::Write::write(&mut self.writer, buf)
}
async fn flush(&mut self) -> io::Result<()> {
std::io::Write::flush(&mut self.writer)
}
async fn shutdown(&mut self) -> io::Result<()> {
Ok(())
}
}
Conversion to AsyncWrite:
let writer = AsyncWriteBytes::new();
let mut async_write = PollWrite::new(writer);
async_write.write_all(b"hello world").await.unwrap();
assert_eq!(async_write.into_inner().written(), b"hello world");