5 个版本
0.2.0 | 2023 年 12 月 31 日 |
---|---|
0.1.5 | 2023 年 10 月 23 日 |
0.1.3 | 2023 年 6 月 14 日 |
#623 in 异步
21KB
516 行
async_async_io
目前仅支持 tokio
.
impl_trait_in_assoc_type
(可选)
- 使用 nightly 频道;
- 启用功能
"impl_trait_in_assoc_type"
; - 查看
src/read.rs
和src/write.rs
中的测试以了解如何实现特性。
使用方法
AsyncAsyncRead
定义
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)
}
}
转换为 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");
AsyncAsyncWrite
定义
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(())
}
}
转换为 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");
依赖项
~2–10MB
~93K SLoC