-
Notifications
You must be signed in to change notification settings - Fork 304
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
API to peek at integers of buffer #382
Comments
Peeking is a bit harder. There are a few options. First, you can take Another alternative is something I proposed in #300, but this would work only for "bufs" that are in continuous memory. |
@carllerche Those are fair points. Maybe this could be a separate trait implemented only by buffers that abstract over contiguous memory, such as This is a bit unrelated, but I wanted to give you an idea since my use case involves My use case of this in implementing the Currently I work around this limitation with the following pattern, exploiting the fact that let mut buf: BytesMut = ...;
// ...
let mut tmp_buf = buf.bytes();
let payload_len = tmp_buf.get_u32() as usize;
// check that we got the full payload in the buffer (NOTE: we need
// to add the message length prefix's byte count to payload_len
// since the buffer cursor was not advanced and thus we need to
// consider the prefix too)
if buf.remaining() >= 4 + payload_len {
// we have the full message in the buffer so advance the buffer
// cursor past the message length header
buf.advance(4);
} else {
return Ok(None);
} I would think this is a common pattern as I'm sure other protocols also employ length prefixes/headers. Is there a better way to do this? |
I'm new to rust. May be this is a solution?
|
This is a feature request for the
Buf
trait to provide an API to peek at an integer of the buffer without advancing the underlying cursor. E.g. there isget_u32
and it would be really useful to have the equivalent but non cursor advancingpeek_u32
that returns the nextu32
of buffer.If this feature already exists, my apologies, but I seem to have missed it. If not, is there any reason it is not included?
The text was updated successfully, but these errors were encountered: