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

Avoid extraneous call to remaining in get_u8/i8 #490

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/buf/buf_impl.rs
Expand Up @@ -125,8 +125,9 @@ pub trait Buf {
/// # Implementer notes
///
/// This function should never panic. Once the end of the buffer is reached,
/// i.e., `Buf::remaining` returns 0, calls to `chunk()` should return an
/// empty slice.
/// i.e., `Buf::remaining` returns 0, calls to `chunk()` must return an
/// empty slice. If there is remaining data, calls must return a slice with at
/// least 1 byte.
// The `chunk` method was previously called `bytes`. This alias makes the rename
// more easily discoverable.
#[cfg_attr(docsrs, doc(alias = "bytes"))]
Expand Down Expand Up @@ -285,7 +286,7 @@ pub trait Buf {
///
/// This function panics if there is no more remaining data in `self`.
fn get_u8(&mut self) -> u8 {
assert!(self.remaining() >= 1);
assert!(self.chunk().len() >= 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, the index bounds check happens just after this, I must assume these asserts are mostly to give a better panic message. Perhaps we could just make them debug_asserts?

@carllerche you added these asserts originally, I think?

let ret = self.chunk()[0];
self.advance(1);
ret
Expand All @@ -308,7 +309,7 @@ pub trait Buf {
///
/// This function panics if there is no more remaining data in `self`.
fn get_i8(&mut self) -> i8 {
assert!(self.remaining() >= 1);
assert!(self.chunk().len() >= 1);
let ret = self.chunk()[0] as i8;
self.advance(1);
ret
Expand Down