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

impl BufMut for ReadBuf (?) #5588

Closed
coolreader18 opened this issue Mar 29, 2023 · 1 comment
Closed

impl BufMut for ReadBuf (?) #5588

coolreader18 opened this issue Mar 29, 2023 · 1 comment
Labels
A-tokio Area: The main tokio crate C-feature-request Category: A feature request. M-io Module: tokio/io

Comments

@coolreader18
Copy link

I came across this bit of code in hyper:

                let copy_len = cmp::min(prefix.len(), buf.remaining());
                // TODO: There should be a way to do following two lines cleaner...
                buf.put_slice(&prefix[..copy_len]);
                prefix.advance(copy_len);

(where prefix: Bytes and buf: &mut ReadBuf<'_>), and realized that this could be made (somewhat) simpler if ReadBuf implemented bytes::BufMut:

                let prefix_buf = (&mut prefix).take(buf.remaining_mut());
                buf.put(prefix_buf);

Is there an API reason ReadBuf doesn't implement BufMut under cfg_io_util! {}? I saw #3884 but that seems to be the opposite direction and is also a helper method, rather than a cfg()'d impl. Not sure if tokio would wanna do those, since it could maybe be more of an API hazard. An alternative is to have a like fn as_buf_mut(&mut self) -> ReadBufBufMut helper, either in tokio or in tokio-util. IMO this is core enough that it's not necessary to stick it intokio-util, since tokio depends on BufMut already and it's a very simple mapping from ReadBuf -> BufMut (like, 15 LOC).

@coolreader18 coolreader18 added A-tokio Area: The main tokio crate C-feature-request Category: A feature request. labels Mar 29, 2023
@Darksonn Darksonn added the M-io Module: tokio/io label Mar 30, 2023
@Darksonn
Copy link
Contributor

Sure, sounds good to me. I'll accept a PR for this if anyone submits it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-feature-request Category: A feature request. M-io Module: tokio/io
Projects
None yet
Development

No branches or pull requests

2 participants