async_async_io

Crates.ioasync_async_io
lib.rsasync_async_io
version
sourcesrc
created_at2023-06-13 11:30:28.981185
updated_at2024-11-01 12:18:29.524421
description`AsyncRead`, `AsyncWrite` traits but with `async fn` methods.
homepage
repositoryhttps://github.com/Banyc/async_async_io
max_upload_size
id888939
Cargo.toml error:TOML parse error at line 17, column 1 | 17 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
IchHabeKeineNamen (Banyc)

documentation

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:

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");
Commit count: 14

cargo fmt