1
0
Fork 0
mirror of https://codeberg.org/icewind/bitbuffer.git synced 2026-06-03 16:44:06 +02:00

clippy fixes

This commit is contained in:
Robin Appelman 2022-04-09 18:41:39 +02:00
commit bd7a4fca5b
8 changed files with 25 additions and 33 deletions

View file

@ -393,21 +393,20 @@ impl SplitFitUsize for usize {
type Iter = array::IntoIter<(usize, u8), 2>;
fn split_fit_usize<E: Endianness>(self) -> Self::Iter {
const USIZE_BITS: usize = size_of::<usize>() * 8;
(if E::is_le() {
[
(
(self & (Self::MAX >> (USIZE_BITS - 8))) as usize,
USIZE_BITS as u8 - 8,
(self & (Self::MAX >> (usize::BITS - 8))) as usize,
usize::BITS as u8 - 8,
),
((self >> (USIZE_BITS - 8)) as usize, 8),
((self >> (usize::BITS - 8)) as usize, 8),
]
} else {
[
((self >> (USIZE_BITS - 8)) as usize, 8),
((self >> (usize::BITS - 8)) as usize, 8),
(
(self & (Self::MAX >> (USIZE_BITS - 8))) as usize,
USIZE_BITS as u8 - 8,
(self & (Self::MAX >> (usize::BITS - 8))) as usize,
usize::BITS as u8 - 8,
),
]
})

View file

@ -126,17 +126,17 @@ macro_rules! impl_read_int {
impl<E: Endianness> BitRead<'_, E> for $type {
#[inline]
fn read(stream: &mut BitReadStream<E>) -> Result<$type> {
stream.read_int::<$type>(size_of::<$type>() * 8)
stream.read_int::<$type>(<$type>::BITS as usize)
}
#[inline]
unsafe fn read_unchecked(stream: &mut BitReadStream<E>, end: bool) -> Result<$type> {
Ok(stream.read_int_unchecked::<$type>(size_of::<$type>() * 8, end))
Ok(stream.read_int_unchecked::<$type>(<$type>::BITS as usize, end))
}
#[inline]
fn bit_size() -> Option<usize> {
Some(size_of::<$type>() * 8)
Some(<$type>::BITS as usize)
}
}
};

View file

@ -197,14 +197,12 @@ pub(crate) fn get_bits_from_usize<E: Endianness>(
bit_offset: usize,
count: usize,
) -> usize {
let usize_bit_size = size_of::<usize>() * 8;
let shifted = if E::is_le() {
val >> bit_offset
} else {
val >> (usize_bit_size - bit_offset - count)
val >> (usize::BITS as usize - bit_offset - count)
};
let mask = !(std::usize::MAX << count);
let mask = !(usize::MAX << count);
shifted & mask
}
@ -388,11 +386,10 @@ where
T: PrimInt + BitOrAssign + IsSigned + UncheckedPrimitiveInt + BitXor,
{
let type_bit_size = size_of::<T>() * 8;
let usize_bit_size = size_of::<usize>() * 8;
let bit_offset = position & 7;
let fit_usize = count + bit_offset < usize_bit_size;
let fit_usize = count + bit_offset < usize::BITS as usize;
let value = if fit_usize {
self.read_fit_usize(position, count, end)
} else {

View file

@ -1,6 +1,5 @@
use crate::{BitReadStream, BitWriteStream, Endianness, Result};
use std::borrow::Cow;
use std::mem::size_of;
use std::rc::Rc;
use std::sync::Arc;
@ -90,7 +89,7 @@ macro_rules! impl_write_int {
impl<E: Endianness> BitWrite<E> for $type {
#[inline]
fn write(&self, stream: &mut BitWriteStream<E>) -> Result<()> {
stream.write_int::<$type>(*self, size_of::<$type>() * 8)
stream.write_int::<$type>(*self, <$type>::BITS as usize)
}
}
};

View file

@ -1,9 +1,6 @@
use crate::Endianness;
use std::cmp::min;
use std::marker::PhantomData;
use std::mem::size_of;
const USIZE_BITS: usize = size_of::<usize>() * 8;
pub struct WriteBuffer<'a, E: Endianness> {
bit_len: usize,
@ -47,11 +44,11 @@ impl<'a, E: Endianness> WriteBuffer<'a, E> {
}
// ensure there are no stray bits
let bits = bits & (usize::MAX >> (USIZE_BITS - count));
let bits = bits & (usize::MAX >> (usize::BITS as usize - count));
let bit_offset = self.bit_len & 7;
debug_assert!(count <= USIZE_BITS - bit_offset);
debug_assert!(count <= usize::BITS as usize - bit_offset);
let last_written_byte = if bit_offset > 0 {
self.bytes.pop().unwrap_or(0)
@ -65,8 +62,8 @@ impl<'a, E: Endianness> WriteBuffer<'a, E> {
self.bytes
.extend_from_slice(&merged.to_le_bytes()[0..merged_byte_count]);
} else {
let merged = ((last_written_byte as usize) << (USIZE_BITS - 8))
| (bits << (USIZE_BITS - bit_offset - count));
let merged = ((last_written_byte as usize) << (usize::BITS as usize - 8))
| (bits << (usize::BITS as usize - bit_offset - count));
self.bytes
.extend_from_slice(&merged.to_be_bytes()[0..merged_byte_count]);
}

View file

@ -270,13 +270,13 @@ where
requested_length: length,
});
}
self.write_bytes(&string.as_bytes())?;
self.write_bytes(string.as_bytes())?;
for _ in 0..(length - string.len()) {
self.push_bits(0, 8)
}
}
None => {
self.write_bytes(&string.as_bytes())?;
self.write_bytes(string.as_bytes())?;
self.push_bits(0, 8)
}
}