Trait CollectionMut

Source
pub trait CollectionMut: Collection {
    type IterMut<'i>: Iterator<Item = &'i mut Self::Item>
       where Self: 'i;

    // Required method
    fn iter_mut(&mut self) -> Self::IterMut<'_>;

    // Provided methods
    fn chained_mut<'a, I>(
        &'a mut self,
        other: &'a mut I,
    ) -> ChainedCol<Self, I, &'a mut Self, &'a mut I>
       where Self: Sized,
             I: CollectionMut<Item = Self::Item> { ... }
    fn filtered_mut<P>(&mut self, filter: P) -> FilteredCol<Self, &mut Self, P>
       where Self: Sized,
             P: Fn(&Self::Item) -> bool + Copy { ... }
    fn flattened_mut(&mut self) -> FlattenedCol<Self, &mut Self>
       where Self: Sized,
             Self::Item: IntoIterator,
             &'i Self::Item: for<'i> IntoIterator<Item = &'i <Self::Item as IntoIterator>::Item>,
             &'i mut Self::Item: for<'i> IntoIterator<Item = &'i mut <Self::Item as IntoIterator>::Item> { ... }
    fn fused_mut(&mut self) -> FusedCol<Self, &mut Self>
       where Self: Sized { ... }
    fn reversed_mut(&mut self) -> ReversedCol<Self, &mut Self>
       where Self: Sized,
             <Self::Iterable<'b> as Iterable>::Iter: for<'b> DoubleEndedIterator,
             Self::IterMut<'b>: for<'b> DoubleEndedIterator { ... }
    fn skipped_mut(&mut self, n: usize) -> SkippedCol<Self, &mut Self>
       where Self: Sized { ... }
    fn skipped_while_mut<P>(
        &mut self,
        skip_while: P,
    ) -> SkippedWhileCol<Self, &mut Self, P>
       where Self: Sized,
             P: Fn(&Self::Item) -> bool + Copy { ... }
    fn stepped_by_mut(&mut self, step: usize) -> SteppedByCol<Self, &mut Self>
       where Self: Sized { ... }
    fn taken_mut(&mut self, n: usize) -> TakenCol<Self, &mut Self>
       where Self: Sized { ... }
    fn taken_while_mut<P>(
        &mut self,
        take_while: P,
    ) -> TakenWhileCol<Self, &mut Self, P>
       where Self: Sized,
             P: Fn(&Self::Item) -> bool + Copy { ... }
}
Expand description

A mutable collection providing the iter_mut method which returns an iterator over mutable references of elements of the collection.

Since it extends Collection, iter method is also available which returns an iterator over shared references of elements.

ยงAuto Implementations

Consider a collection type X storing elements of type T. Provided that the following implementations are provided:

  • X: IntoIterator<Item = T>
  • &X: IntoIterator<Item = &T>
  • &mut X: IntoIterator<Item = &mut T>

Then, X implements Collection<Item = T> and CollectionMut<Item = T>. Further, &X implements Iterable<Item = &T>.

ยงExamples

use orx_iterable::*;
use arrayvec::ArrayVec;
use smallvec::{smallvec, SmallVec};
use std::collections::{LinkedList, VecDeque};

/// first computes sum, and then adds it to each of the elements
fn increment_by_sum(numbers: &mut impl CollectionMut<Item = i32>) {
    let sum: i32 = numbers.iter().sum();

    for x in numbers.iter_mut() {
        *x += sum;
    }
}

// example collections that automatically implement CollectionMut

let mut x = [1, 2, 3];
increment_by_sum(&mut x);
assert_eq!(x, [7, 8, 9]);

let mut x = vec![1, 2, 3];
increment_by_sum(&mut x);

let mut x = LinkedList::from_iter([1, 2, 3]);
increment_by_sum(&mut x);

let mut x = VecDeque::from_iter([1, 2, 3]);
increment_by_sum(&mut x);

let mut x: SmallVec<[_; 128]> = smallvec![3, 5, 7];
increment_by_sum(&mut x);

let mut x = ArrayVec::<_, 16>::new();
x.extend([3, 5, 7]);
increment_by_sum(&mut x);

Required Associated Typesยง

Source

type IterMut<'i>: Iterator<Item = &'i mut Self::Item> where Self: 'i

Type of the iterator yielding mutable references created by the iter_mut method.

Required Methodsยง

Source

fn iter_mut(&mut self) -> Self::IterMut<'_>

Creates a new iterator yielding mutable references to the elements of the collection; i.e., type of elements is &mut Collection::Item.

Provided Methodsยง

Source

fn chained_mut<'a, I>( &'a mut self, other: &'a mut I, ) -> ChainedCol<Self, I, &'a mut Self, &'a mut I>
where Self: Sized, I: CollectionMut<Item = Self::Item>,

Combines mutable references of this collection and other; and creates an iterable collection which is a chain of these two collections.

Note that this method does not change the memory locations of the elements; i.e., the elements still live in two separate collections; however, now chained together.

ยงExamples
use orx_iterable::*;

let mut a = vec!['a', 'b'];
let mut b = ['c', 'd', 'e'];

let mut it = a.chained_mut(&mut b);

*it.iter_mut().last().unwrap() = 'x';

assert_eq!(it.iter().count(), 5);
assert_eq!(it.iter().collect::<Vec<_>>(), vec![&'a', &'b', &'c', &'d', &'x']);

// neither a nor b is consumed
assert_eq!(a, ['a', 'b']);
assert_eq!(b, ['c', 'd', 'x']);
Source

fn filtered_mut<P>(&mut self, filter: P) -> FilteredCol<Self, &mut Self, P>
where Self: Sized, P: Fn(&Self::Item) -> bool + Copy,

Creates an iterable collection view which is a filtered version of this collection from its mutable reference.

ยงExamples
use orx_iterable::*;

let mut a = [0i32, 1, 2];

let mut it = a.filtered_mut(|x| x.is_positive());

for x in it.iter_mut() {
    *x *= 2;
}

assert_eq!(it.iter().count(), 2);
assert_eq!(it.iter().collect::<Vec<_>>(), [&2, &4]);

// a is not consumed
assert_eq!(a, [0, 2, 4]);
Source

fn flattened_mut(&mut self) -> FlattenedCol<Self, &mut Self>
where Self: Sized, Self::Item: IntoIterator, &'i Self::Item: for<'i> IntoIterator<Item = &'i <Self::Item as IntoIterator>::Item>, &'i mut Self::Item: for<'i> IntoIterator<Item = &'i mut <Self::Item as IntoIterator>::Item>,

Creates an iterable collection view which is a flattened version of this collection from its mutable reference.

ยงExamples
use orx_iterable::*;

let mut data = vec![vec![1, 2, 3, 4], vec![5, 6]];

let mut it = data.flattened_mut();

for x in it.iter_mut() {
    *x *= 2;
}

assert_eq!(it.iter().count(), 6);
assert_eq!(it.iter().sum::<u32>(), 2 * 21);

// data is not consumed
assert_eq!(data, [vec![2, 4, 6, 8], vec![10, 12]]);
Source

fn fused_mut(&mut self) -> FusedCol<Self, &mut Self>
where Self: Sized,

Creates an iterable collection view which is a fused version of this collection from its mutable reference.

See core::iter::Fuse for details on fused iterators.

Source

fn reversed_mut(&mut self) -> ReversedCol<Self, &mut Self>
where Self: Sized, <Self::Iterable<'b> as Iterable>::Iter: for<'b> DoubleEndedIterator, Self::IterMut<'b>: for<'b> DoubleEndedIterator,

Creates an iterable collection view which is a reversed version of this collection from its mutable reference.

ยงExamples
use orx_iterable::*;

let mut data = vec![vec![1, 2, 3, 4], vec![5, 6]];

let mut a = [1, 2, 3];

let mut it = a.reversed_mut();
*it.iter_mut().next().unwrap() += 10;
assert_eq!(it.iter().collect::<Vec<_>>(), [&13, &2, &1]);
Source

fn skipped_mut(&mut self, n: usize) -> SkippedCol<Self, &mut Self>
where Self: Sized,

Creates an iterable collection view which is skipped-by-n version of this collection from its mutable reference.

ยงExamples
use orx_iterable::*;

let mut a = [1, 2, 3, 4, 5];

let mut it = a.skipped_mut(2);

for x in it.iter_mut() {
    *x += 10;
}

assert_eq!(it.iter().collect::<Vec<_>>(), [&13, &14, &15]);

assert_eq!(a, [1, 2, 13, 14, 15]);
Source

fn skipped_while_mut<P>( &mut self, skip_while: P, ) -> SkippedWhileCol<Self, &mut Self, P>
where Self: Sized, P: Fn(&Self::Item) -> bool + Copy,

Creates an iterable collection view which is skipped-while version of this collection from its mutable reference.

ยงExamples
use orx_iterable::*;

let mut a = [-1i32, 0, 1];

let mut it = a.skipped_while_mut(|x| x.is_negative());

for x in it.iter_mut() {
    *x += 10;
}

assert_eq!(it.iter().collect::<Vec<_>>(), [&10, &11]);

assert_eq!(a, [-1, 10, 11]);
Source

fn stepped_by_mut(&mut self, step: usize) -> SteppedByCol<Self, &mut Self>
where Self: Sized,

Creates an iterable collection view which is stepped-by-step version of this collection from its mutable reference.

ยงExamples
use orx_iterable::*;

let mut a = [0, 1, 2, 3, 4, 5];

let mut it = a.stepped_by_mut(2);

for x in it.iter_mut() {
    *x *= 10;
}

assert_eq!(it.iter().collect::<Vec<_>>(), [&0, &20, &40]);

assert_eq!(a, [0, 1, 20, 3, 40, 5]);
Source

fn taken_mut(&mut self, n: usize) -> TakenCol<Self, &mut Self>
where Self: Sized,

Creates an iterable collection view which is taken-n version of this collection from its mutable reference.

ยงExamples
use orx_iterable::*;

let mut a = [1, 2, 3, 4, 5];

let mut it = a.taken_mut(3);

for x in it.iter_mut() {
    *x += 10;
}

assert_eq!(it.iter().collect::<Vec<_>>(), [&11, &12, &13]);

assert_eq!(a, [11, 12, 13, 4, 5]);
Source

fn taken_while_mut<P>( &mut self, take_while: P, ) -> TakenWhileCol<Self, &mut Self, P>
where Self: Sized, P: Fn(&Self::Item) -> bool + Copy,

Creates an iterable collection view which is taken-while version of this collection from its mutable reference.

ยงExamples
use orx_iterable::*;

let mut a = [-1i32, 0, 1];

let mut it = a.taken_while_mut(|x| x.is_negative());

for x in it.iter_mut() {
    *x *= 10;
}

assert_eq!(it.iter().collect::<Vec<_>>(), [&-10]);

assert_eq!(a, [-10, 0, 1]);

Dyn Compatibilityยง

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementorsยง

Sourceยง

impl<I1, I2, E1, E2> CollectionMut for ChainedCol<I1, I2, E1, E2>
where I1: CollectionMut, I2: CollectionMut<Item = <I1 as Collection>::Item>, E1: SoM<I1>, E2: SoM<I2>,

Sourceยง

type IterMut<'i> = Chain<<I1 as CollectionMut>::IterMut<'i>, <I2 as CollectionMut>::IterMut<'i>> where ChainedCol<I1, I2, E1, E2>: 'i

Sourceยง

impl<I, E> CollectionMut for FlattenedCol<I, E>
where I: CollectionMut, <I as Collection>::Item: IntoIterator, &'i <I as Collection>::Item: for<'i> IntoIterator<Item = &'i <<I as Collection>::Item as IntoIterator>::Item>, &'i mut <I as Collection>::Item: for<'i> IntoIterator<Item = &'i mut <<I as Collection>::Item as IntoIterator>::Item>, E: SoM<I>,

Sourceยง

type IterMut<'i> = Flatten<<I as CollectionMut>::IterMut<'i>> where FlattenedCol<I, E>: 'i

Sourceยง

impl<I, E> CollectionMut for FusedCol<I, E>
where I: CollectionMut, E: SoM<I>,

Sourceยง

type IterMut<'i> = Fuse<<I as CollectionMut>::IterMut<'i>> where FusedCol<I, E>: 'i

Sourceยง

impl<I, E> CollectionMut for ReversedCol<I, E>
where I: CollectionMut, E: SoM<I>, <<I as Collection>::Iterable<'b> as Iterable>::Iter: for<'b> DoubleEndedIterator, <I as CollectionMut>::IterMut<'b>: for<'b> DoubleEndedIterator,

Sourceยง

type IterMut<'i> = Rev<<I as CollectionMut>::IterMut<'i>> where ReversedCol<I, E>: 'i

Sourceยง

impl<I, E> CollectionMut for SkippedCol<I, E>
where I: CollectionMut, E: SoM<I>,

Sourceยง

type IterMut<'i> = Skip<<I as CollectionMut>::IterMut<'i>> where SkippedCol<I, E>: 'i

Sourceยง

impl<I, E> CollectionMut for SteppedByCol<I, E>
where I: CollectionMut, E: SoM<I>,

Sourceยง

type IterMut<'i> = StepBy<<I as CollectionMut>::IterMut<'i>> where SteppedByCol<I, E>: 'i

Sourceยง

impl<I, E> CollectionMut for TakenCol<I, E>
where I: CollectionMut, E: SoM<I>,

Sourceยง

type IterMut<'i> = Take<<I as CollectionMut>::IterMut<'i>> where TakenCol<I, E>: 'i

Sourceยง

impl<I, E, P> CollectionMut for FilteredCol<I, E, P>
where I: CollectionMut, E: SoM<I>, P: Fn(&<I as Collection>::Item) -> bool + Copy,

Sourceยง

type IterMut<'i> = FilteredColIterMut<'i, I, P> where FilteredCol<I, E, P>: 'i

Sourceยง

impl<I, E, P> CollectionMut for SkippedWhileCol<I, E, P>
where I: CollectionMut, E: SoM<I>, P: Fn(&<I as Collection>::Item) -> bool + Copy,

Sourceยง

type IterMut<'i> = SkippedWhileColIterMut<'i, I, P> where SkippedWhileCol<I, E, P>: 'i

Sourceยง

impl<I, E, P> CollectionMut for TakenWhileCol<I, E, P>
where I: CollectionMut, E: SoM<I>, P: Fn(&<I as Collection>::Item) -> bool + Copy,

Sourceยง

type IterMut<'i> = TakenWhileColIterMut<'i, I, P> where TakenWhileCol<I, E, P>: 'i

Sourceยง

impl<T> CollectionMut for EmptyCol<T>

Sourceยง

type IterMut<'i> = Empty<&'i mut T> where EmptyCol<T>: 'i

Sourceยง

impl<T> CollectionMut for OnceCol<T>

Sourceยง

type IterMut<'i> = Once<&'i mut T> where OnceCol<T>: 'i

Sourceยง

impl<X> CollectionMut for X
where &'a X: for<'a> IntoIterator<Item = &'a <X as IntoIterator>::Item>, &'a mut X: for<'a> IntoIterator<Item = &'a mut <X as IntoIterator>::Item>, X: IntoIterator,

Sourceยง

type IterMut<'i> = <&'i mut X as IntoIterator>::IntoIter where X: 'i