Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add chunks() for BoundedBuf #269

Open
ileixe opened this issue Jun 1, 2023 · 0 comments
Open

Add chunks() for BoundedBuf #269

ileixe opened this issue Jun 1, 2023 · 0 comments

Comments

@ileixe
Copy link

ileixe commented Jun 1, 2023

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)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant