mirror of
https://codeberg.org/icewind/bitbuffer.git
synced 2026-06-03 16:44:06 +02:00
remove bounds check using unsafe
This commit is contained in:
parent
ccc48f1bdb
commit
7250ce3ae8
1 changed files with 10 additions and 9 deletions
|
|
@ -89,9 +89,16 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_usize_bytes(&self, byte_index: usize) -> [u8; USIZE_SIZE] {
|
fn read_usize_bytes(&self, byte_index: usize) -> [u8; USIZE_SIZE] {
|
||||||
self.bytes[byte_index..byte_index + USIZE_SIZE]
|
debug_assert!(byte_index + USIZE_SIZE < self.bytes.len());
|
||||||
.try_into()
|
// this is safe because all calling paths check that byte_index is less than the unpadded
|
||||||
.unwrap()
|
// length (because they check based on bit_len), so with padding byte_index + USIZE_SIZE is
|
||||||
|
// always within bounds
|
||||||
|
unsafe {
|
||||||
|
self.bytes
|
||||||
|
.get_unchecked(byte_index..byte_index + USIZE_SIZE)
|
||||||
|
.try_into()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// note that only the bottom USIZE - 1 bytes are usable
|
/// note that only the bottom USIZE - 1 bytes are usable
|
||||||
|
|
@ -237,9 +244,6 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Panics:
|
|
||||||
///
|
|
||||||
/// requires position + count to not go out of bounds of the buffer: ((position + count) / 8) <= self.bytes.len()
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn read_fit_usize<T>(&self, position: usize, count: usize) -> T
|
fn read_fit_usize<T>(&self, position: usize, count: usize) -> T
|
||||||
where
|
where
|
||||||
|
|
@ -249,9 +253,6 @@ where
|
||||||
T::from_unchecked(raw)
|
T::from_unchecked(raw)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Panics:
|
|
||||||
///
|
|
||||||
/// requires position + count to not go out of bounds of the buffer: ((position + count) / 8) <= self.bytes.len()
|
|
||||||
fn read_no_fit_usize<T>(&self, position: usize, count: usize) -> T
|
fn read_no_fit_usize<T>(&self, position: usize, count: usize) -> T
|
||||||
where
|
where
|
||||||
T: PrimInt + BitOrAssign + IsSigned + UncheckedPrimitiveInt,
|
T: PrimInt + BitOrAssign + IsSigned + UncheckedPrimitiveInt,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue