BlockMut

Struct BlockMut 

Source
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>

Source

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.

Source

pub fn rows(&self) -> usize

Number of rows in this block.

Source

pub fn cols(&self) -> usize

Number of columns in this block.

Source

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);
Source

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.

Source

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);
Source

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.

Source

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);
Source

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.

Source

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.

Source

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());
Source

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());
Source

pub fn cast_const(self) -> BlockRef<'data, T>

Turn this unique reference into a shared reference.

Source

pub fn reborrow(&mut self) -> BlockMut<'_, T>

Create a unique reference to this block with a shorter lifetime.

Source

pub fn iter_rows(self) -> IterRows<'data, T>

Iterate over the rows of this block.

Source

pub fn iter_rows_mut(self) -> IterRowsMut<'data, T>

Iterate over the rows of this block.

Source

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>>

Source

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.

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T> Index<(usize, usize)> for BlockMut<'_, T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: (usize, usize)) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> IndexMut<(usize, usize)> for BlockMut<'_, T>

Source§

fn index_mut(&mut self, index: (usize, usize)) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<T> Send for BlockMut<'_, T>
where T: Sync,

Source§

impl<T> Sync for BlockMut<'_, T>
where T: Sync,

Auto Trait Implementations§

§

impl<'a, T> Freeze for BlockMut<'a, T>

§

impl<'a, T> RefUnwindSafe for BlockMut<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Unpin for BlockMut<'a, T>

§

impl<'a, T> !UnwindSafe for BlockMut<'a, T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.