mirror of
https://codeberg.org/icewind/bitbuffer.git
synced 2026-06-03 16:44:06 +02:00
safe
This commit is contained in:
parent
c28c83d5f4
commit
7290481a73
3 changed files with 9 additions and 25 deletions
|
|
@ -9,4 +9,3 @@ matrix:
|
|||
fast_finish: true
|
||||
script:
|
||||
- cargo test --all
|
||||
- cargo test --all --all-features
|
||||
|
|
@ -20,6 +20,3 @@ memchr = "2.2"
|
|||
maplit = "1.0.1"
|
||||
|
||||
[workspace]
|
||||
|
||||
[features]
|
||||
unsafe = []
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue