tokio-rs / tokio-uring

An io_uring backed runtime for Rust
MIT License
1.11k stars 117 forks source link

Add chunks() for BoundedBuf #269

Open ileixe opened 1 year ago

ileixe commented 1 year ago

We have usage to write chunks from BoundedBuf like below

use tokio_uring::buf::BoundedBuf;

fn write_boundedbuf<B: BoundedBuf>(buf: B) -> (usize, B) {
    todo!();
}

fn chunk_write_boundedbuf<B>(buf: B)
where
    B: BoundedBuf,
    <B as BoundedBuf>::Bounds: Clone,
{
    let chunk_size = 2;

    let total = buf.bytes_init();
    let bounds = buf.bounds();
    let mut slice = buf.slice(..);
    let mut written = 0;

    while written < total {
        slice = slice.into_inner().slice(bounds.clone()).slice(written..);
        let (n, new) = write_boundedbuf(slice.slice(..chunk_size));
        slice = new;
        written += n;
    }
}

In fact, what I want is to write chunks from a whole buffer. In slice case, it's just

fn write(buf: &[u8]) -> usize {
    todo!();
}

fn chunk_write(buf: &[u8]) {
    for chunk in buf.chunks(2) {
        write(chunk);
    }
}

The boundedBuf usage is not only very verbose but also error prune. Can we have additional functions in https://doc.rust-lang.org/std/slice/index.html (to be specific, Chunks for our case)?