[−][src]Struct ingrid::RowMut
A mutable view onto a row of a grid
This structure is an mutable view into a row of a grid and its lifetime is bound to the lifetime of the grid. It's a lightweight construct that allows to operate on individual rows effectively; see it as an equivalent to the slice primitive for grids.
Instead of accessing elements with coordinates, just an index is needed. Rows use the left to right direction, therefore, index zero corresponds to the element at the very left, also denoted the 'first' element of the row. Note that rows are indexable.
With a row, you can easily retrieve the left and right elements of the row,
with the left()
and right()
methods, but also retrieve the row above or
below, with the top()
and bottom()
methods. You can also conveniently
iterate over its elements with the iterator()
method which returns an
efficient iterator.
Unlike their immutable counter-part, there are additional operations you can do such as reversing the elements of the row, or rotate them to the left or the right.
Examples
Iterating over the elements of a row.
let mut grid = Grid::from_rows(vec![vec![1, 2, 3], vec![4, 5, 6]]); let row = grid.row_mut(0); for (coordinate, value) in row.iterator().enumerate_coordinate() { println!("Element at {:?} has value {}.", coordinate, *value); }
Indexing the row.
let mut grid = Grid::from_rows(vec![vec![1, 2, 3], vec![4, 5, 6]]); println!("First element of first row is {}", grid.row_mut(0)[0]); println!("Last element of last row is {}", grid.row_mut(1)[2]);
Fields
grid: &'a mut Grid<T>
A reference to its grid.
index: usize
The index of the row.
Methods
impl<'a, T: Clone> RowMut<'a, T>
[src]
pub fn length(&self) -> usize
[src]
Returns the length of the row.
This method returns the length of the row which is the number of elements. It's equivalent to the width of the grid.
Examples
let mut grid = Grid::with_size(size!(3, 2), 42); assert_eq!(grid.row_mut(0).length(), 3); assert_eq!(grid.row_mut(1).length(), 3); assert_eq!(grid.size().width, 3);
pub fn value(&self, index: usize) -> &T
[src]
Returns a reference to an element of the row.
This method returns a reference to an element of the row from its index.
Note that index zero corresponds to the element at the very left (the
first element of the row). If you're looking to get the first or the
last elements of the row, check out the left()
and right()
methods.
Arguments
index
- Index of the element
Panics
It panics if the index is out of bounds.
Examples
let mut grid = Grid::from_rows(vec![vec![1, 2, 3], vec![4, 5, 6]]); let row = grid.row_mut(1); assert_eq!(row.value(0), &4); assert_eq!(row.value(1), &5); assert_eq!(row.value(2), &6); row.value(3); // It panics here !
pub fn value_mut(&mut self, index: usize) -> &mut T
[src]
Returns a mutable reference to an element of the row.
This method returns a mutable reference to an element of the row from its index.
Arguments
index
- Index of the element
Panics
It panics if the index is out of bounds.
Examples
let mut grid = Grid::from_rows(vec![vec![1, 2, 3], vec![4, 0, 6]]); let mut row = grid.row_mut(1); *row.value_mut(1) = 5; assert_eq!(row.value(0), &4); assert_eq!(row.value(1), &5); assert_eq!(row.value(2), &6); row.value(3); // It panics here !
pub fn set_value(&mut self, index: usize, value: T)
[src]
Replace an element of the row.
This method replaces the value of an element of the row from its index and a new value, effectively dropping the previous value.
Arguments
index
- Index of the elementvalue
- New value of the element
Panics
It panics if the index is out of bounds.
Examples
let mut grid = Grid::from_rows(vec![vec![1, 2, 3], vec![4, 0, 6]]); let mut row = grid.row_mut(1); row.set_value(1, 5); assert_eq!(row.value(0), &4); assert_eq!(row.value(1), &5); assert_eq!(row.value(2), &6); row.set_value(3, 42); // It panics here !
pub fn swap_value(&mut self, a: usize, b: usize)
[src]
Swap two elements of the row.
This method swaps two elements of the row from their index.
Arguments
a
- Index of one of the element to swapb
- Index of the other element to be swapped with
Panics
It panics if the indexes are out of bounds.
Examples
let mut grid = Grid::from_rows(vec![vec![1, 2, 3], vec![6, 5, 4]]); let mut row = grid.row_mut(1); row.swap_value(0, 2); assert_eq!(row.value(0), &4); assert_eq!(row.value(1), &5); assert_eq!(row.value(2), &6); row.swap_value(1, 3); // It panics here !
pub fn values(&self) -> Vec<&T>
[src]
Return the elements of the row.
This method returns the elements of the row as a vector of reference.
Examples
let mut grid = Grid::from_rows(vec![vec![1, 2], vec![3, 4]]); assert_eq!(grid.row_mut(0).values(), vec![&1, &2]); assert_eq!(grid.row_mut(1).values(), vec![&3, &4]);
pub fn left(&self) -> &T
[src]
Returns a reference to the first element of the row.
This method returns a reference to the first element of the row. It's
equivalent to retrieving the element with index 0
.
Note that there is always a first element or the grid would have no size.
Examples
let mut grid = Grid::from_rows(vec![vec![1, 2, 3], vec![4, 5, 6]]); // The first element of the second row is 4. let row = grid.row_mut(1); assert_eq!(row.left(), &4);
pub fn left_mut(&mut self) -> &mut T
[src]
Returns a mutable reference to the first element of the row.
This method returns a mutable reference to the first element of the
row. It's equivalent to retrieving the element with index 0
.
Note that there is always a first element or the grid would have no size.
Examples
let mut grid = Grid::from_rows(vec![vec![1, 2, 3], vec![0, 5, 6]]); // The first element of the second row is 0 but we can change it to 4. let mut row = grid.row_mut(1); *row.left_mut() = 4; assert_eq!(row.left(), &4);
pub fn right(&self) -> &T
[src]
Returns a reference to the last element of the row.
This method returns a reference to the last element of the row. It's
equivalent to retrieving the element with index length() -1
.
Note that there is always a last element or the grid would have no size.
Examples
let mut grid = Grid::from_rows(vec![vec![1, 2, 3], vec![4, 5, 6]]); // The last element of the second row is 6. let row = grid.row_mut(1); assert_eq!(row.right(), &6);
pub fn right_mut(&mut self) -> &mut T
[src]
Returns a mutable reference to the last element of the row.
This method returns a mutable reference to the last element of the row.
It's equivalent to retrieving the element with index length() -1
.
Note that there is always a last element or the grid would have no size.
Examples
let mut grid = Grid::from_rows(vec![vec![1, 2, 3], vec![4, 5, 0]]); // The last element of the second row is 0 but we can change it to 6. let mut row = grid.row_mut(1); *row.right_mut() = 6; assert_eq!(row.right_mut(), &6);
ⓘImportant traits for IteratorRow<'a, T>pub fn iterator(&'a self) -> IteratorRow<'a, T>
[src]
Returns an iterator over the row.
This method returns an iterator over the row.
Examples
let mut grid = Grid::from_rows(vec![vec![ 1, 2, 3], vec![42, 42, 42]]); // Check if all elements of the row have value 42. assert_eq!(grid.row_mut(0).iterator().all(|item| *item == 42), false); assert_eq!(grid.row_mut(1).iterator().all(|item| *item == 42), true);
pub fn top(&'a self) -> Option<Row<'a, T>>
[src]
Returns the row above.
This method returns the row above this row, or None
if this is already
the row at the very top of the grid.
Examples
let mut grid = Grid::from_rows(vec![vec![1, 2], vec![3, 4]]); let second_row = grid.row_mut(1); let first_row = second_row.top().unwrap(); assert!(first_row.top().is_none()); // There is no row above.
pub fn top_mut(&'a mut self) -> Option<RowMut<'a, T>>
[src]
Returns the mutable row above.
This method returns the mutable row above this row, or None
if this is
already the row at the very top of the grid.
Examples
let mut grid = Grid::from_rows(vec![vec![1, 2], vec![3, 4]]); let mut second_row = grid.row_mut(1); let mut first_row = second_row.top_mut().unwrap(); assert!(first_row.top_mut().is_none()); // There is no row above.
pub fn bottom(&'a self) -> Option<Row<'a, T>>
[src]
Returns the row below.
This method returns the row below this row, or None
if this is already
the row at the very bottom of the grid.
Examples
let mut grid = Grid::from_rows(vec![vec![1, 2], vec![3, 4]]); let mut first_row = grid.row_mut(0); let second_row = first_row.bottom().unwrap(); assert!(second_row.bottom().is_none()); // There is no row below.
pub fn bottom_mut(&'a mut self) -> Option<RowMut<'a, T>>
[src]
Returns the mutable row below.
This method returns the mutable row below this row, or None
if this is
already the row at the very bottom of the grid.
Examples
let mut grid = Grid::from_rows(vec![vec![1, 2], vec![3, 4]]); let mut first_row = grid.row_mut(0); let mut second_row = first_row.bottom_mut().unwrap(); assert!(second_row.bottom_mut().is_none()); // There is no row below.
pub fn reverse(&mut self)
[src]
Reverse the order of the elements.
This method reverses the order of the elements in the row, in place.
Note that it's similar to the reverse()
method of the slice primitive
type.
Examples
let mut grid = Grid::from_rows(vec![vec![1, 2, 3], vec![6, 5, 4]]); let mut row = grid.row_mut(1); row.reverse(); assert_eq!(row.value(0), &4); assert_eq!(row.value(1), &5); assert_eq!(row.value(2), &6);
pub fn rotate_left(&mut self, number: usize)
[src]
Rotate elements to the left.
This method rotates the row in-place such that the elements are moved
a given number
of times to the left. The elements that goes out of the
row are added back to the right of the row.
Note that it's similar to the rotate_left()
method of the slice
primitive type.
Arguments
- number - The number of times elements are rotated
Panics
This function will panic if number
is greater than the length of the
row.
Examples
let mut grid = Grid::from_rows(vec![vec![1, 2, 3], vec![4, 5, 6]]); let mut row = grid.row_mut(1); row.rotate_left(1); assert_eq!(row.value(0), &5); assert_eq!(row.value(1), &6); assert_eq!(row.value(2), &4);
pub fn rotate_right(&mut self, number: usize)
[src]
Rotate elements to the right.
This method rotates the row in-place such that the elements are moved
a given number
of times to the right. The elements that goes out of
the row are added back to the left of the row.
Note that it's similar to the rotate_right()
method of the slice
primitive type.
Arguments
- number - The number of times elements are rotated
Panics
This function will panic if number
is greater than the length of the
row.
Examples
let mut grid = Grid::from_rows(vec![vec![1, 2, 3], vec![4, 5, 6]]); let mut row = grid.row_mut(1); row.rotate_right(1); assert_eq!(row.value(0), &6); assert_eq!(row.value(1), &4); assert_eq!(row.value(2), &5);
pub fn swap(&mut self, a: usize, b: usize)
[src]
Swap two elements in the row.
This method swaps two elements in the row.
Note that it's similar to the swap()
method of the slice primitive
type.
Arguments
- a - The index of the first element
- b - The index of the second element
Panics
It panics if a
or b
are out of bounds.
Examples
let mut grid = Grid::from_rows(vec![vec![1, 2, 3], vec![6, 5, 4]]); let mut row = grid.row_mut(1); row.swap(0, 2); assert_eq!(row.value(0), &4); assert_eq!(row.value(1), &5); assert_eq!(row.value(2), &6);
Trait Implementations
impl<'a, T: Debug> Debug for RowMut<'a, T>
[src]
impl<'a, T: Eq> Eq for RowMut<'a, T>
[src]
impl<'a, T: Clone> Index<usize> for RowMut<'a, T>
[src]
type Output = T
The returned type after indexing.
fn index(&self, index: usize) -> &Self::Output
[src]
impl<'a, T: Clone> IndexMut<usize> for RowMut<'a, T>
[src]
impl<'a, T: PartialEq> PartialEq<RowMut<'a, T>> for RowMut<'a, T>
[src]
impl<'a, T> StructuralEq for RowMut<'a, T>
[src]
impl<'a, T> StructuralPartialEq for RowMut<'a, T>
[src]
Auto Trait Implementations
impl<'a, T> RefUnwindSafe for RowMut<'a, T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
impl<'a, T> Send for RowMut<'a, T> where
T: Send,
T: Send,
impl<'a, T> Sync for RowMut<'a, T> where
T: Sync,
T: Sync,
impl<'a, T> Unpin for RowMut<'a, T>
impl<'a, T> !UnwindSafe for RowMut<'a, T>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,