Removed unnecessary `unsafe` markers

This commit is contained in:
Violeta Hernández 2021-07-08 11:12:43 -05:00
parent 40bc1cb304
commit 04e937792b
4 changed files with 41 additions and 11 deletions

View File

@ -79,7 +79,7 @@ where
} }
#[inline] #[inline]
unsafe fn is_contiguous(&self) -> bool { fn is_contiguous(&self) -> bool {
true true
} }

View File

@ -166,7 +166,7 @@ macro_rules! storage_impl(
} }
#[inline] #[inline]
unsafe fn is_contiguous(&self) -> bool { fn is_contiguous(&self) -> bool {
// Common cases that can be deduced at compile-time even if one of the dimensions // Common cases that can be deduced at compile-time even if one of the dimensions
// is Dynamic. // is Dynamic.
if (RStride::is::<U1>() && C::is::<U1>()) || // Column vector. if (RStride::is::<U1>() && C::is::<U1>()) || // Column vector.

View File

@ -70,24 +70,36 @@ pub unsafe trait Storage<T: 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.
///
/// # Safety
/// If the index is out of bounds, dereferencing the result will cause undefined behavior.
#[inline] #[inline]
unsafe fn get_address_unchecked_linear(&self, i: usize) -> *const T { fn get_address_unchecked_linear(&self, i: usize) -> *const T {
self.ptr().wrapping_add(i) self.ptr().wrapping_add(i)
} }
/// 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.
///
/// # Safety
/// If the index is out of bounds, dereferencing the result will cause undefined behavior.
#[inline] #[inline]
unsafe fn get_address_unchecked(&self, irow: usize, icol: usize) -> *const T { fn get_address_unchecked(&self, irow: usize, icol: usize) -> *const T {
self.get_address_unchecked_linear(self.linear_index(irow, icol)) self.get_address_unchecked_linear(self.linear_index(irow, icol))
} }
/// Retrieves a reference to the i-th element without bound-checking. /// Retrieves a reference to the i-th element without bound-checking.
///
/// # Safety
/// If the index is out of bounds, the method will cause undefined behavior.
#[inline] #[inline]
unsafe fn get_unchecked_linear(&self, i: usize) -> &T { unsafe fn get_unchecked_linear(&self, i: usize) -> &T {
&*self.get_address_unchecked_linear(i) &*self.get_address_unchecked_linear(i)
} }
/// Retrieves a reference to the i-th element without bound-checking. /// Retrieves a reference to the i-th element without bound-checking.
///
/// # Safety
/// If the index is out of bounds, the method will cause undefined behavior.
#[inline] #[inline]
unsafe fn get_unchecked(&self, irow: usize, icol: usize) -> &T { unsafe fn get_unchecked(&self, irow: usize, icol: usize) -> &T {
self.get_unchecked_linear(self.linear_index(irow, icol)) self.get_unchecked_linear(self.linear_index(irow, icol))
@ -96,9 +108,9 @@ pub unsafe trait Storage<T: Scalar, R: Dim, C: Dim = U1>: Debug + Sized {
/// Indicates whether this data buffer stores its elements contiguously. /// Indicates whether this data buffer stores its elements contiguously.
/// ///
/// # Safety /// # Safety
/// This method is unsafe because unsafe code relies on this properties to performe /// This function must not return `true` if the underlying storage is not contiguous,
/// some low-lever optimizations. /// or undefined behaviour will occur.
unsafe fn is_contiguous(&self) -> bool; fn is_contiguous(&self) -> bool;
/// Retrieves the data buffer as a contiguous slice. /// Retrieves the data buffer as a contiguous slice.
/// ///
@ -131,30 +143,45 @@ pub unsafe trait StorageMut<T: Scalar, R: Dim, C: Dim = U1>: Storage<T, R, C> {
fn ptr_mut(&mut self) -> *mut T; fn ptr_mut(&mut self) -> *mut T;
/// 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.
///
/// # Safety
/// If the index is out of bounds, dereferencing the result will cause undefined behavior.
#[inline] #[inline]
unsafe fn get_address_unchecked_linear_mut(&mut self, i: usize) -> *mut T { fn get_address_unchecked_linear_mut(&mut self, i: usize) -> *mut T {
self.ptr_mut().wrapping_add(i) self.ptr_mut().wrapping_add(i)
} }
/// 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.
///
/// # Safety
/// If the index is out of bounds, dereferencing the result will cause undefined behavior.
#[inline] #[inline]
unsafe fn get_address_unchecked_mut(&mut self, irow: usize, icol: usize) -> *mut T { fn get_address_unchecked_mut(&mut self, irow: usize, icol: usize) -> *mut T {
let lid = self.linear_index(irow, icol); let lid = self.linear_index(irow, icol);
self.get_address_unchecked_linear_mut(lid) self.get_address_unchecked_linear_mut(lid)
} }
/// Retrieves a mutable reference to the i-th element without bound-checking. /// Retrieves a mutable reference to the i-th element without bound-checking.
///
/// # Safety
/// If the index is out of bounds, the method will cause undefined behavior.
unsafe fn get_unchecked_linear_mut(&mut self, i: usize) -> &mut T { unsafe fn get_unchecked_linear_mut(&mut self, i: usize) -> &mut T {
&mut *self.get_address_unchecked_linear_mut(i) &mut *self.get_address_unchecked_linear_mut(i)
} }
/// Retrieves a mutable reference to the element at `(irow, icol)` without bound-checking. /// Retrieves a mutable reference to the element at `(irow, icol)` without bound-checking.
///
/// # Safety
/// If the index is out of bounds, the method will cause undefined behavior.
#[inline] #[inline]
unsafe fn get_unchecked_mut(&mut self, irow: usize, icol: usize) -> &mut T { unsafe fn get_unchecked_mut(&mut self, irow: usize, icol: usize) -> &mut T {
&mut *self.get_address_unchecked_mut(irow, icol) &mut *self.get_address_unchecked_mut(irow, icol)
} }
/// Swaps two elements using their linear index without bound-checking. /// Swaps two elements using their linear index without bound-checking.
///
/// # Safety
/// If the indices are out of bounds, the method will cause undefined behavior.
#[inline] #[inline]
unsafe fn swap_unchecked_linear(&mut self, i1: usize, i2: usize) { unsafe fn swap_unchecked_linear(&mut self, i1: usize, i2: usize) {
let a = self.get_address_unchecked_linear_mut(i1); let a = self.get_address_unchecked_linear_mut(i1);
@ -164,6 +191,9 @@ pub unsafe trait StorageMut<T: Scalar, R: Dim, C: Dim = U1>: Storage<T, R, C> {
} }
/// Swaps two elements without bound-checking. /// Swaps two elements without bound-checking.
///
/// # Safety
/// If the indices are out of bounds, the method will cause undefined behavior.
#[inline] #[inline]
unsafe fn swap_unchecked(&mut self, row_col1: (usize, usize), row_col2: (usize, usize)) { unsafe fn swap_unchecked(&mut self, row_col1: (usize, usize), row_col2: (usize, usize)) {
let lid1 = self.linear_index(row_col1.0, row_col1.1); let lid1 = self.linear_index(row_col1.0, row_col1.1);

View File

@ -180,7 +180,7 @@ where
} }
#[inline] #[inline]
unsafe fn is_contiguous(&self) -> bool { fn is_contiguous(&self) -> bool {
true true
} }
@ -229,7 +229,7 @@ where
} }
#[inline] #[inline]
unsafe fn is_contiguous(&self) -> bool { fn is_contiguous(&self) -> bool {
true true
} }