Skip to content

Commit

Permalink
use cast for ArrayProxy
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Oct 18, 2023
1 parent c339d85 commit 2e62581
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/generate/array_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,20 @@ pub struct ArrayProxy<T, const COUNT: usize, const STRIDE: usize> {
#[allow(clippy::len_without_is_empty)]
impl<T, const C: usize, const S: usize> ArrayProxy<T, C, S> {
/// Get a reference from an [ArrayProxy] with no bounds checking.
pub unsafe fn get_ref(&self, index: usize) -> &T {
let base = self as *const Self as usize;
let address = base + S * index;
&*(address as *const T)
pub const unsafe fn get_ref(&self, index: usize) -> &T {
&*(self as *const Self).cast::<u8>().add(S * index).cast::<T>()
}
/// Get a reference from an [ArrayProxy], or return `None` if the index
/// is out of bounds.
pub fn get(&self, index: usize) -> Option<&T> {
pub const fn get(&self, index: usize) -> Option<&T> {
if index < C {
Some(unsafe { self.get_ref(index) })
} else {
None
}
}
/// Return the number of items.
pub fn len(&self) -> usize {
pub const fn len(&self) -> usize {
C
}
}
Expand Down

0 comments on commit 2e62581

Please sign in to comment.