Skip to content

Commit

Permalink
added bytes-specific get_X functions to reduce dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
Walnut356 committed Aug 16, 2023
1 parent 64c4fa2 commit 74e7628
Showing 1 changed file with 113 additions and 0 deletions.
113 changes: 113 additions & 0 deletions src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,23 @@ impl Clone for Bytes {
}
}

macro_rules! buf_get_impl {
($this:ident, $typ:tt::$conv:tt) => {{
const SIZE: usize = mem::size_of::<$typ>();
// 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 = $this
.chunk()
.get(..SIZE)
.map(|src| unsafe { $typ::$conv(*(src as *const _ as *const [_; SIZE])) })
.unwrap();

// 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 @@ -567,6 +584,102 @@ impl Buf for Bytes {
ret
}
}

fn get_u16(&mut self) -> u16 {
buf_get_impl!(self, u16::from_be_bytes);
}

fn get_u16_le(&mut self) -> u16 {
buf_get_impl!(self, u16::from_le_bytes);
}

fn get_u16_ne(&mut self) -> u16 {
buf_get_impl!(self, u16::from_ne_bytes);
}

fn get_i16(&mut self) -> i16 {
buf_get_impl!(self, i16::from_be_bytes);
}

fn get_i16_le(&mut self) -> i16 {
buf_get_impl!(self, i16::from_le_bytes);
}

fn get_i16_ne(&mut self) -> i16 {
buf_get_impl!(self, i16::from_ne_bytes);
}

fn get_u32(&mut self) -> u32 {
buf_get_impl!(self, u32::from_be_bytes);
}

fn get_u32_le(&mut self) -> u32 {
buf_get_impl!(self, u32::from_le_bytes);
}

fn get_u32_ne(&mut self) -> u32 {
buf_get_impl!(self, u32::from_ne_bytes);
}

fn get_i32(&mut self) -> i32 {
buf_get_impl!(self, i32::from_be_bytes);
}

fn get_i32_le(&mut self) -> i32 {
buf_get_impl!(self, i32::from_le_bytes);
}

fn get_i32_ne(&mut self) -> i32 {
buf_get_impl!(self, i32::from_ne_bytes);
}

fn get_u64(&mut self) -> u64 {
buf_get_impl!(self, u64::from_be_bytes);
}

fn get_u64_le(&mut self) -> u64 {
buf_get_impl!(self, u64::from_le_bytes);
}

fn get_u64_ne(&mut self) -> u64 {
buf_get_impl!(self, u64::from_ne_bytes);
}

fn get_i64(&mut self) -> i64 {
buf_get_impl!(self, i64::from_be_bytes);
}

fn get_i64_le(&mut self) -> i64 {
buf_get_impl!(self, i64::from_le_bytes);
}

fn get_i64_ne(&mut self) -> i64 {
buf_get_impl!(self, i64::from_ne_bytes);
}

fn get_u128(&mut self) -> u128 {
buf_get_impl!(self, u128::from_be_bytes);
}

fn get_u128_le(&mut self) -> u128 {
buf_get_impl!(self, u128::from_le_bytes);
}

fn get_u128_ne(&mut self) -> u128 {
buf_get_impl!(self, u128::from_ne_bytes);
}

fn get_i128(&mut self) -> i128 {
buf_get_impl!(self, i128::from_be_bytes);
}

fn get_i128_le(&mut self) -> i128 {
buf_get_impl!(self, i128::from_le_bytes);
}

fn get_i128_ne(&mut self) -> i128 {
buf_get_impl!(self, i128::from_ne_bytes);
}
}

impl Deref for Bytes {
Expand Down

0 comments on commit 74e7628

Please sign in to comment.