Skip to main content

MatMut

Type Alias MatMut 

pub type MatMut<'a, T, Rows = usize, Cols = usize, RStride = isize, CStride = isize> = Mat<Mut<'a, T, Rows, Cols, RStride, CStride>>;
Expand description

mutable view over a matrix, similar to a mutable reference to a 2d strided slice

§note

unlike a slice, the data pointed to by MatMut<'_, T> is allowed to be partially or fully uninitialized under certain conditions. In this case, care must be taken to not perform any operations that read the uninitialized values, either directly or indirectly through any of the numerical library routines, unless it is explicitly permitted

§move semantics

since MatMut mutably borrows data, it cannot be Copy. this means that if we pass a MatMut to a function that takes it by value, or use a method that consumes self like [MatMut::transpose_mut], this renders the original variable unusable

ⓘ
use faer::{Mat, MatMut};

fn takes_matmut(view: MatMut<'_, f64>) {}

let mut matrix = Mat::new();
let view = matrix.as_mut();

takes_matmut(view); // `view` is moved (passed by value)
takes_matmut(view); // this fails to compile since `view` was moved

the way to get around it is to use the [reborrow::ReborrowMut] trait, which allows us to mutably borrow a MatMut to obtain another MatMut for the lifetime of the borrow. it’s also similarly possible to immutably borrow a MatMut to obtain a MatRef for the lifetime of the borrow, using [reborrow::Reborrow] ``` use faer::{Mat, MatMut, MatRef}; use reborrow::*; fn takes_matmut(view: MatMut<‘, f64>) {} fn takes_matref(view: MatRef<’, f64>) {} let mut matrix = Mat::new(); let mut view = matrix.as_mut(); takes_matmut(view.rb_mut()); takes_matmut(view.rb_mut()); takes_matref(view.rb()); // view is still usable here

Aliased Type§

#[repr(transparent)]
pub struct MatMut<'a, T, Rows = usize, Cols = usize, RStride = isize, CStride = isize>(pub Mut<'a, T, Rows, Cols, RStride, CStride>);

Tuple Fields§

§0: Mut<'a, T, Rows, Cols, RStride, CStride>