Switch to `wrapping_offset` instead of unsafe `offset`

This commit is contained in:
Aaron Hill 2019-11-19 17:42:33 -05:00
parent 8dd673d48b
commit df87769988
No known key found for this signature in database
GPG Key ID: B4087E510E98B164
1 changed files with 2 additions and 20 deletions

View File

@ -72,16 +72,7 @@ pub unsafe trait Storage<N: Scalar, R: Dim, C: Dim = U1>: Debug + Sized {
/// Gets the address of the i-th matrix component without performing bound-checking. /// Gets the address of the i-th matrix component without performing bound-checking.
#[inline] #[inline]
unsafe fn get_address_unchecked_linear(&self, i: usize) -> *const N { unsafe fn get_address_unchecked_linear(&self, i: usize) -> *const N {
let shape = self.shape(); self.ptr().wrapping_offset(i as isize)
if shape.0.value() * shape.1.value() == 0 {
// If we have a zero-size matrix, our pointer must
// be dangling. Instead of calling 'offset', we
// just re-use our pointer, since actually using
// it would be undefined behavior
self.ptr()
} else {
self.ptr().offset(i as isize)
}
} }
/// Gets the address of the i-th matrix component without performing bound-checking. /// Gets the address of the i-th matrix component without performing bound-checking.
@ -133,16 +124,7 @@ pub unsafe trait StorageMut<N: Scalar, R: Dim, C: Dim = U1>: Storage<N, R, C> {
/// Gets the mutable address of the i-th matrix component without performing bound-checking. /// Gets the mutable address of the i-th matrix component without performing bound-checking.
#[inline] #[inline]
unsafe fn get_address_unchecked_linear_mut(&mut self, i: usize) -> *mut N { unsafe fn get_address_unchecked_linear_mut(&mut self, i: usize) -> *mut N {
let shape = self.shape(); self.ptr_mut().wrapping_offset(i as isize)
if shape.0.value() * shape.1.value() == 0 {
// If we have a zero-size matrix, our pointer must
// be dangling. Instead of calling 'offset', we
// just re-use our pointer, since actually using
// it would be undefined behavior
self.ptr_mut()
} else {
self.ptr_mut().offset(i as isize)
}
} }
/// Gets the mutable address of the i-th matrix component without performing bound-checking. /// Gets the mutable address of the i-th matrix component without performing bound-checking.