Trait nalgebra::base::storage::RawStorage

source ·
pub unsafe trait RawStorage<T, R: Dim, C: Dim = U1>: Sized {
    type RStride: Dim;
    type CStride: Dim;

    // Required methods
    fn ptr(&self) -> *const T;
    fn shape(&self) -> (R, C);
    fn strides(&self) -> (Self::RStride, Self::CStride);
    fn is_contiguous(&self) -> bool;
    unsafe fn as_slice_unchecked(&self) -> &[T];

    // Provided methods
    fn linear_index(&self, irow: usize, icol: usize) -> usize { ... }
    fn get_address_unchecked_linear(&self, i: usize) -> *const T { ... }
    fn get_address_unchecked(&self, irow: usize, icol: usize) -> *const T { ... }
    unsafe fn get_unchecked_linear(&self, i: usize) -> &T { ... }
    unsafe fn get_unchecked(&self, irow: usize, icol: usize) -> &T { ... }
}
Expand description

The trait shared by all matrix data storage.

TODO: doc In generic code, it is recommended use the Storage trait bound instead. The RawStorage trait bound is generally used by code that needs to work with storages that contains MaybeUninit<T> elements.

Note that Self must always have a number of elements compatible with the matrix length (given by R and C if they are known at compile-time). For example, implementors of this trait should not allow the user to modify the size of the underlying buffer with safe methods (for example the VecStorage::data_mut method is unsafe because the user could change the vector’s size so that it no longer contains enough elements: this will lead to UB.

Required Associated Types§

source

type RStride: Dim

The static stride of this storage’s rows.

source

type CStride: Dim

The static stride of this storage’s columns.

Required Methods§

source

fn ptr(&self) -> *const T

The matrix data pointer.

source

fn shape(&self) -> (R, C)

The dimension of the matrix at run-time. Arr length of zero indicates the additive identity element of any dimension. Must be equal to Self::dimension() if it is not None.

source

fn strides(&self) -> (Self::RStride, Self::CStride)

The spacing between consecutive row elements and consecutive column elements.

For example this returns (1, 5) for a row-major matrix with 5 columns.

source

fn is_contiguous(&self) -> bool

Indicates whether this data buffer stores its elements contiguously.

§Safety

This function must not return true if the underlying storage is not contiguous, or undefined behaviour will occur.

source

unsafe fn as_slice_unchecked(&self) -> &[T]

Retrieves the data buffer as a contiguous slice.

§Safety

The matrix components may not be stored in a contiguous way, depending on the strides. This method is unsafe because this can yield to invalid aliasing when called on some pairs of matrix views originating from the same matrix with strides.

Call the safe alternative matrix.as_slice() instead.

Provided Methods§

source

fn linear_index(&self, irow: usize, icol: usize) -> usize

Compute the index corresponding to the irow-th row and icol-th column of this matrix. The index must be such that the following holds:

let lindex = self.linear_index(irow, icol);
assert!(*self.get_unchecked(irow, icol) == *self.get_unchecked_linear(lindex))
source

fn get_address_unchecked_linear(&self, i: usize) -> *const T

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.

source

fn get_address_unchecked(&self, irow: usize, icol: usize) -> *const T

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.

source

unsafe fn get_unchecked_linear(&self, i: usize) -> &T

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.

source

unsafe fn get_unchecked(&self, irow: usize, icol: usize) -> &T

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.

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<'a, T, R: Dim, C: Dim, RStride: Dim, CStride: Dim> RawStorage<T, R, C> for ViewStorage<'a, T, R, C, RStride, CStride>

§

type RStride = RStride

§

type CStride = CStride

source§

impl<'a, T, R: Dim, C: Dim, RStride: Dim, CStride: Dim> RawStorage<T, R, C> for ViewStorageMut<'a, T, R, C, RStride, CStride>

§

type RStride = RStride

§

type CStride = CStride

source§

impl<T, C: Dim> RawStorage<T, Dyn, C> for VecStorage<T, Dyn, C>

§

type RStride = Const<1>

§

type CStride = Dyn

source§

impl<T, R: DimName> RawStorage<T, R, Dyn> for VecStorage<T, R, Dyn>

§

type RStride = Const<1>

§

type CStride = R

source§

impl<T, const R: usize, const C: usize> RawStorage<T, Const<R>, Const<C>> for ArrayStorage<T, R, C>

§

type RStride = Const<1>

§

type CStride = Const<R>