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

remove usafe code from read_bool

safe version is a bit faster than the unsafe version even
This commit is contained in:
Robin Appelman 2019-07-24 12:24:15 +02:00
commit 76bd8f6bb1
2 changed files with 28 additions and 8 deletions

View file

@ -148,16 +148,16 @@ where
let byte_index = position / 8;
let bit_offset = position & 7;
if position >= self.bit_len {
return Err(ReadError::NotEnoughData {
self.bytes
.get(byte_index)
.ok_or_else(|| ReadError::NotEnoughData {
requested: 1,
bits_left: self.bit_len - position,
});
}
let byte = unsafe { self.bytes.get_unchecked(byte_index) };
let shifted = byte >> bit_offset;
Ok(shifted & 1u8 == 1)
})
.map(|byte| {
let shifted = byte >> bit_offset;
shifted & 1u8 == 1
})
}
/// Read a sequence of bits from the buffer as integer