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

async_async_io

AsyncRead, AsyncWrite traits but with async fn methods

8 releases

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

#436 in Asynchronous

Download history 12/week @ 2024-07-23 1/week @ 2024-07-30 5/week @ 2024-08-27 7/week @ 2024-09-03 6/week @ 2024-09-10 6/week @ 2024-09-17 24/week @ 2024-09-24 8/week @ 2024-10-01 7/week @ 2024-10-08 10/week @ 2024-10-15 358/week @ 2024-10-29 57/week @ 2024-11-05

428 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–11MB
~92K SLoC