#async-io #async-read #async-write #tokio #async-trait #fn #traits

async_async_io

AsyncRead, AsyncWrite traits but with async fn methods

8 releases

new 0.2.3 Nov 1, 2024
0.2.2 Nov 1, 2024
0.2.1 Oct 31, 2024
0.2.0 Dec 31, 2023
0.1.3 Jun 14, 2023

#407 in Asynchronous

Download history 2/week @ 2024-07-19 11/week @ 2024-07-26 1/week @ 2024-08-23 7/week @ 2024-08-30 5/week @ 2024-09-06 7/week @ 2024-09-13 15/week @ 2024-09-20 18/week @ 2024-09-27 6/week @ 2024-10-04 10/week @ 2024-10-11 4/week @ 2024-10-18 62/week @ 2024-10-25

86 downloads per month

MIT license

23KB
562 lines

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:

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");

AsyncAsyncWrite

Definition:

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");

Dependencies

~2.3–10MB
~92K SLoC