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

@ -8,5 +8,4 @@ matrix:
- rust: nightly - rust: nightly
fast_finish: true fast_finish: true
script: script:
- cargo test --all - cargo test --all
- cargo test --all --all-features

View file

@ -20,6 +20,3 @@ memchr = "2.2"
maplit = "1.0.1" maplit = "1.0.1"
[workspace] [workspace]
[features]
unsafe = []

View file

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