1
0
Fork 0
mirror of https://codeberg.org/icewind/bitbuffer.git synced 2026-06-03 16:44:06 +02:00
This commit is contained in:
Robin Appelman 2019-12-25 23:59:43 +01:00
commit 7290481a73
3 changed files with 9 additions and 25 deletions

View file

@ -86,27 +86,15 @@ where
}
fn read_usize_bytes(&self, byte_index: usize) -> [u8; USIZE_SIZE] {
if cfg!(feature = "unsafe") {
use std::mem::transmute;
// panic instead of accessing out of bounds data when the caller didn't do it's job bounds checking
// due to the magic of branch prediction and this check "always" passing, the cost for this
// is below the point of being measurable by `cargo bench`
unsafe {
let ptr = self.bytes.as_ptr().add(byte_index);
*transmute::<_, &[u8; USIZE_SIZE]>(ptr)
}
if byte_index + USIZE_SIZE <= self.bytes.len() {
self.bytes[byte_index..byte_index + USIZE_SIZE]
.try_into()
.unwrap()
} else {
if byte_index + USIZE_SIZE <= self.bytes.len() {
self.bytes[byte_index..byte_index + USIZE_SIZE]
.try_into()
.unwrap()
} else {
let mut bytes = [0; USIZE_SIZE];
let copy_bytes = self.bytes.len() - byte_index;
bytes[0..copy_bytes]
.copy_from_slice(&self.bytes[byte_index..byte_index + copy_bytes]);
bytes
}
let mut bytes = [0; USIZE_SIZE];
let copy_bytes = self.bytes.len() - byte_index;
bytes[0..copy_bytes].copy_from_slice(&self.bytes[byte_index..byte_index + copy_bytes]);
bytes
}
}