Function extendr_api::prelude::mat::from_raw_parts
pub unsafe fn from_raw_parts<'a, E>(
ptr: <<E as Entity>::Group as ForType>::FaerOf<*const <E as Entity>::Unit>,
nrows: usize,
ncols: usize,
row_stride: isize,
col_stride: isize,
) -> MatRef<'a, E>where
E: Entity,
Expand description
Creates a MatRef
from pointers to the matrix data, dimensions, and strides.
The row (resp. column) stride is the offset from the memory address of a given matrix
element at indices (row: i, col: j)
, to the memory address of the matrix element at
indices (row: i + 1, col: 0)
(resp. (row: 0, col: i + 1)
). This offset is specified in
number of elements, not in bytes.
§Safety
The behavior is undefined if any of the following conditions are violated:
- For each matrix unit, the entire memory region addressed by the matrix must be contained
within a single allocation, accessible in its entirety by the corresponding pointer in
ptr
. - For each matrix unit, the corresponding pointer must be properly aligned, even for a zero-sized matrix.
- The values accessible by the matrix must be initialized at some point before they are read, or references to them are formed.
- No mutable aliasing is allowed. In other words, none of the elements accessible by any
matrix unit may be accessed for writes by any other means for the duration of the lifetime
'a
.
§Example
use faer::mat;
// row major matrix with 2 rows, 3 columns, with a column at the end that we want to skip.
// the row stride is the pointer offset from the address of 1.0 to the address of 4.0,
// which is 4.
// the column stride is the pointer offset from the address of 1.0 to the address of 2.0,
// which is 1.
let data = [[1.0, 2.0, 3.0, f64::NAN], [4.0, 5.0, 6.0, f64::NAN]];
let matrix = unsafe { mat::from_raw_parts::<f64>(data.as_ptr() as *const f64, 2, 3, 4, 1) };
let expected = mat![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]];
assert_eq!(expected.as_ref(), matrix);