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

Resolving remaining pedantic clippy issues #29

Merged
merged 1 commit into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ macro_rules! base_reader {
($which:ident) => {
#[inline]
unsafe fn read(&mut self) -> u64 {
core::ptr::read_unaligned(self.bit_ptr as *const u64).$which()
self.bit_ptr.cast::<u64>().read_unaligned().$which()
}

#[inline]
Expand Down Expand Up @@ -611,6 +611,7 @@ macro_rules! generate_bitter_end {

impl<'a> $name<'a> {
#[inline]
#[must_use]
pub fn new(data: &'a [u8]) -> Self {
let input_marker = data.len().saturating_sub(7);
let rng = data.as_ptr_range();
Expand Down Expand Up @@ -657,10 +658,10 @@ macro_rules! generate_bitter_end {

#[inline]
unsafe fn read_eof(&mut self) -> u64 {
let mut result: u64 = 0;
let mut result = [0u8; 8];
let len = self.unbuffered_bytes();
core::ptr::copy_nonoverlapping(self.bit_ptr, &mut result as *mut u64 as *mut u8, len);
result.$which()
self.bit_ptr.copy_to_nonoverlapping(result.as_mut_ptr(), len);
u64::from_ne_bytes(result).$which()
}

// End of buffer checking is a fascinating topic, see:
Expand Down Expand Up @@ -812,15 +813,18 @@ pub type NativeEndianReader<'a> = BigEndianReader<'a>;
/// bits.consume(bits_to_read);
/// ```
#[inline]
#[must_use]
pub fn sign_extend(val: u64, bits: u32) -> i64 {
// Branchless sign extension from bit twiddling hacks:
// https://graphics.stanford.edu/~seander/bithacks.html#VariableSignExtend
//
// The 3 operation approach with division turned out to be significantly slower,
// and so was not used.
debug_assert!(val.leading_zeros() >= (BIT_WIDTH as u32 - bits));
debug_assert!(val.leading_zeros() as usize >= (BIT_WIDTH - bits as usize));
let m = 1i64.wrapping_shl(bits.wrapping_sub(1));
((val as i64) ^ m) - m
#[allow(clippy::cast_possible_wrap)]
let val = val as i64;
(val ^ m) - m
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion tests/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ fn _test_bit_reads2<T: BitReader>(mut bitter: T, bits: u32) {

let res = bitter.peek(chunk);
bitter.consume(chunk);
bitter::sign_extend(res, len);
let _ = bitter::sign_extend(res, len);
}
}

Expand Down