Skip to content

Commit

Permalink
fix: Use unaligned read as copy_nonoverlapping requires alignment (de…
Browse files Browse the repository at this point in the history
  • Loading branch information
mmastrac committed Jul 11, 2023
1 parent 9d5c21a commit 226c662
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/fast_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,12 @@ impl FastApiOneByteString {
}

impl<T: Default> FastApiTypedArray<T> {
/// Performs an unaligned-safe read of T from the underlying data.
#[inline(always)]
pub fn get(&self, index: usize) -> T {
debug_assert!(index < self.length);
let mut t: T = Default::default();
unsafe {
ptr::copy_nonoverlapping(self.data.add(index), &mut t, 1);
}
t
// SAFETY: src is valid for reads, and is a valid value for T
unsafe { ptr::read_unaligned(self.data.add(index)) }
}

#[inline(always)]
Expand Down

0 comments on commit 226c662

Please sign in to comment.