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 specialized get_X impls for Bytes struct to reduce dead code #622

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
145 changes: 145 additions & 0 deletions src/bytes.rs
Expand Up @@ -531,6 +531,21 @@ impl Clone for Bytes {
}
}

macro_rules! buf_get_impl {
($this:ident, $typ:tt::$conv:tt) => {{
const SIZE: usize = mem::size_of::<$typ>();

assert!($this.len >= SIZE);
// slice.get() returns None on failing bounds check, resulting in a panic, but skips the unnecessary code of the
// default buf impl that needs to account for non-contiguous memory
let ret = unsafe { $typ::$conv(($this.ptr as *const $typ).read_unaligned()) };

// if the direct conversion was possible, advance and return
$this.advance(SIZE);
return ret;
}};
}

impl Buf for Bytes {
#[inline]
fn remaining(&self) -> usize {
Expand Down Expand Up @@ -565,6 +580,136 @@ impl Buf for Bytes {
ret
}
}

#[inline]
fn get_u8(&mut self) -> u8 {
buf_get_impl!(self, u8::from)
}

#[inline]
fn get_i8(&mut self) -> i8 {
buf_get_impl!(self, i8::from)
}

#[inline]
fn get_u16(&mut self) -> u16 {
buf_get_impl!(self, u16::from_be);
}

#[inline]
fn get_u16_le(&mut self) -> u16 {
buf_get_impl!(self, u16::from_le);
}

#[inline]
fn get_u16_ne(&mut self) -> u16 {
buf_get_impl!(self, u16::from);
}

#[inline]
fn get_i16(&mut self) -> i16 {
buf_get_impl!(self, i16::from_be);
}

#[inline]
fn get_i16_le(&mut self) -> i16 {
buf_get_impl!(self, i16::from_le);
}

#[inline]
fn get_i16_ne(&mut self) -> i16 {
buf_get_impl!(self, i16::from);
}

#[inline]
fn get_u32(&mut self) -> u32 {
buf_get_impl!(self, u32::from_be);
}

#[inline]
fn get_u32_le(&mut self) -> u32 {
buf_get_impl!(self, u32::from_le);
}

#[inline]
fn get_u32_ne(&mut self) -> u32 {
buf_get_impl!(self, u32::from);
}

#[inline]
fn get_i32(&mut self) -> i32 {
buf_get_impl!(self, i32::from_be);
}

#[inline]
fn get_i32_le(&mut self) -> i32 {
buf_get_impl!(self, i32::from_le);
}

#[inline]
fn get_i32_ne(&mut self) -> i32 {
buf_get_impl!(self, i32::from);
}

#[inline]
fn get_u64(&mut self) -> u64 {
buf_get_impl!(self, u64::from_be);
}

#[inline]
fn get_u64_le(&mut self) -> u64 {
buf_get_impl!(self, u64::from_le);
}

#[inline]
fn get_u64_ne(&mut self) -> u64 {
buf_get_impl!(self, u64::from);
}

#[inline]
fn get_i64(&mut self) -> i64 {
buf_get_impl!(self, i64::from_be);
}

#[inline]
fn get_i64_le(&mut self) -> i64 {
buf_get_impl!(self, i64::from_le);
}

#[inline]
fn get_i64_ne(&mut self) -> i64 {
buf_get_impl!(self, i64::from);
}

#[inline]
fn get_u128(&mut self) -> u128 {
buf_get_impl!(self, u128::from_be);
}

#[inline]
fn get_u128_le(&mut self) -> u128 {
buf_get_impl!(self, u128::from_le);
}

#[inline]
fn get_u128_ne(&mut self) -> u128 {
buf_get_impl!(self, u128::from);
}

#[inline]
fn get_i128(&mut self) -> i128 {
buf_get_impl!(self, i128::from_be);
}

#[inline]
fn get_i128_le(&mut self) -> i128 {
buf_get_impl!(self, i128::from_le);
}

#[inline]
fn get_i128_ne(&mut self) -> i128 {
buf_get_impl!(self, i128::from);
}
}

impl Deref for Bytes {
Expand Down
32 changes: 32 additions & 0 deletions tests/test_bytes.rs
Expand Up @@ -1208,3 +1208,35 @@ fn test_bytes_capacity_len() {
}
}
}

#[test]
fn test_get_u16() {
let mut buf = Bytes::from(&b"\x21\x54zomg"[..]);
assert_eq!(0x2154, buf.get_u16());
let mut buf = Bytes::from(&b"\x21\x54zomg"[..]);
assert_eq!(0x5421, buf.get_u16_le());
}

#[test]
#[should_panic]
fn test_get_u16_buffer_underflow() {
let mut buf = Bytes::from(&b"\x21"[..]);
buf.get_u16();
}

#[test]
#[should_panic]
fn test_bytes_overread() {
let mut b = Bytes::from_static(&[0, 1, 2]);
let _ = b.get_u32();
}

// running this test would result in a panic without `.read_unaligned()`
// on x86 read_unaligned compiles down to a single `mov`, on platforms with no unaligned access,
// it uses rust's `copy_nonoverlapping`
#[test]
fn test_bytes_misaligned() {
let mut b = Bytes::from_static(&[0, 1, 2, 3, 4, 5, 6, 7, 8]);
b.advance(2);
let _ = b.get_u32();
}