pub struct BlockMut<'a, T> { /* private fields */ }Expand description
A reference to a block of a matrix with unique access to elements.
Implementations§
Source§impl<'data, T> BlockMut<'data, T>
impl<'data, T> BlockMut<'data, T>
Sourcepub fn new(data: &'data mut [T], pitch: usize) -> Self
pub fn new(data: &'data mut [T], pitch: usize) -> Self
Create a new block reference from a raw slice and pitch.
The resulting block refers to the whole matrix.
§Panics
Panics if the length of data is not a multiple of pitch.
Sourcepub fn split_at_col(
self,
mid: usize,
) -> (BlockMut<'data, T>, BlockMut<'data, T>)
pub fn split_at_col( self, mid: usize, ) -> (BlockMut<'data, T>, BlockMut<'data, T>)
Divide into two blocks at the given column.
§Examples
let data = &mut [
[0, 1, 2],
[3, 4, 5],
];
let block = matrix_slice::from_array_rows_mut(data);
let (left, right) = block.split_at_col(2);
assert_eq!(left[(1, 0)], 3);
assert_eq!(right[(1, 0)], 5);Sourcepub fn split_at_col_checked(
self,
mid: usize,
) -> Option<(BlockMut<'data, T>, BlockMut<'data, T>)>
pub fn split_at_col_checked( self, mid: usize, ) -> Option<(BlockMut<'data, T>, BlockMut<'data, T>)>
Divide into two blocks at the given column.
See Self::split_at_col but returns None if out of bounds.
Sourcepub fn split_at_row(
self,
mid: usize,
) -> (BlockMut<'data, T>, BlockMut<'data, T>)
pub fn split_at_row( self, mid: usize, ) -> (BlockMut<'data, T>, BlockMut<'data, T>)
Divide into two blocks at the given row.
§Examples
let data = &mut [
[0, 1, 2],
[3, 4, 5],
];
let block = matrix_slice::from_array_rows_mut(data);
let (top, bot) = block.split_at_row(1);
assert_eq!(top[(0, 2)], 2);
assert_eq!(bot[(0, 2)], 5);Sourcepub fn split_at_row_checked(
self,
mid: usize,
) -> Option<(BlockMut<'data, T>, BlockMut<'data, T>)>
pub fn split_at_row_checked( self, mid: usize, ) -> Option<(BlockMut<'data, T>, BlockMut<'data, T>)>
Divide into two blocks at the given row.
See Self::split_at_row but returns None if out of bounds.
Sourcepub fn row(self, row: usize) -> VecMut<'data, T>
pub fn row(self, row: usize) -> VecMut<'data, T>
Choose a single row and refer to its data.
§Examples
let data = &mut [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
];
let mut block = matrix_slice::from_array_rows_mut(data);
let mut row = block.reborrow().row(1);
row[0] = 0x42;
assert_eq!(block[(1, 0)], 0x42);Sourcepub fn col(self, col: usize) -> VecMut<'data, T>
pub fn col(self, col: usize) -> VecMut<'data, T>
Choose a single column and refer to its data.
§Examples
let data = &mut [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
];
let mut block = matrix_slice::from_array_rows_mut(data);
let mut row = block.reborrow().col(1);
row[0] = 0x42;
assert_eq!(block[(0, 1)], 0x42);Sourcepub fn select_rows<R>(self, range: R) -> Option<BlockMut<'data, T>>where
R: MatrixIndex,
pub fn select_rows<R>(self, range: R) -> Option<BlockMut<'data, T>>where
R: MatrixIndex,
Choose a range of rows and contract the block to that.
The argument type is flexible, allowing ranges (1..3), half open ranges (2.. and ..2)
among others. See the MatrixIndex trait, which is sealed though as its details are not
yet finalized.
§Examples
let data = &mut [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
];
let block = matrix_slice::from_array_rows_mut(data);
let center = block.select_rows(1..2).unwrap();
assert_eq!(center.rows(), 1);
assert_eq!(center.cols(), 3);
assert_eq!(center[(0, 1)], 4);Sourcepub fn select_cols<R>(self, range: R) -> Option<BlockMut<'data, T>>where
R: MatrixIndex,
pub fn select_cols<R>(self, range: R) -> Option<BlockMut<'data, T>>where
R: MatrixIndex,
Choose a range of columns and contract the block to that.
The argument type is flexible, allowing ranges (1..3), half open ranges (2.. and ..2)
among others. See the MatrixIndex trait, which is sealed though as its details are not
yet finalized.
Sourcepub fn select(
self,
row_range: impl MatrixIndex,
col_range: impl MatrixIndex,
) -> Option<BlockMut<'data, T>>
pub fn select( self, row_range: impl MatrixIndex, col_range: impl MatrixIndex, ) -> Option<BlockMut<'data, T>>
Choose a sub-block by its range of rows and columns.
Sourcepub fn into_contiguous_slice(self) -> Option<&'data mut [T]>
pub fn into_contiguous_slice(self) -> Option<&'data mut [T]>
Extract a contiguous underlying slice of elements if the block is contiguous.
§Examples
let data = &mut [[0u32; 3]; 3];
let mut block = matrix_slice::from_array_rows_mut(data);
let (mut part, _) = block.reborrow().split_at_row(2);
assert!(part.into_contiguous_slice().is_some());
let (pre, post) = block.reborrow().split_at_col(2);
assert!(pre.into_contiguous_slice().is_none());
assert!(post.into_contiguous_slice().is_none());
let (same, _) = block.reborrow().split_at_col(3);
assert!(same.into_contiguous_slice().is_some());Sourcepub fn into_array_rows_checked<const N: usize>(
self,
) -> Option<&'data mut [[T; N]]>
pub fn into_array_rows_checked<const N: usize>( self, ) -> Option<&'data mut [[T; N]]>
Extract access as a slice of arrays if the block is contiguous.
The caller must choose N matching the number of columns.
§Examples
let data = &mut [[0u32; 3]; 3];
let mut block = matrix_slice::from_array_rows_mut(data);
// Turns this back into the same type as `data` had.
assert!(block.reborrow().into_array_rows_checked::<3>().is_some());
// Using an incorrect number of columns fails.
assert!(block.reborrow().into_array_rows_checked::<2>().is_none());
// Can still be used after splitting at rows.
let (_, mut block) = block.split_at_row(2);
assert!(block.reborrow().into_array_rows_checked::<3>().is_some());Sourcepub fn cast_const(self) -> BlockRef<'data, T>
pub fn cast_const(self) -> BlockRef<'data, T>
Turn this unique reference into a shared reference.
Sourcepub fn reborrow(&mut self) -> BlockMut<'_, T>
pub fn reborrow(&mut self) -> BlockMut<'_, T>
Create a unique reference to this block with a shorter lifetime.
Sourcepub fn iter_rows_mut(self) -> IterRowsMut<'data, T> ⓘ
pub fn iter_rows_mut(self) -> IterRowsMut<'data, T> ⓘ
Iterate over the rows of this block.
Sourcepub fn as_cells(self) -> BlockMut<'data, Cell<T>>
pub fn as_cells(self) -> BlockMut<'data, Cell<T>>
Modify the item type to a Cell, allowing interior mutability.
This is the equivalent of Cell::from_mut over elements in this slice.
Source§impl<'data, T> BlockMut<'data, Cell<T>>
impl<'data, T> BlockMut<'data, Cell<T>>
Sourcepub fn as_cell_items(self) -> BlockMut<'data, T>
pub fn as_cell_items(self) -> BlockMut<'data, T>
Modify the item type from a Cell to its interior type.
This is the equivalent of Cell::get_mut over elements in this slice.
Trait Implementations§
Source§impl<T> Default for BlockMut<'_, T>
Creates an empty block reference, within a matrix of a dangling slice.
impl<T> Default for BlockMut<'_, T>
Creates an empty block reference, within a matrix of a dangling slice.