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,
             for<'i> &'i Self::Item: IntoIterator<Item = &'i <Self::Item as IntoIterator>::Item>,
             for<'i> &'i mut Self::Item: 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,
             for<'b> <Self::Iterable<'b> as Iterable>::Iter: DoubleEndedIterator,
             for<'b> Self::IterMut<'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, for<'i> &'i Self::Item: IntoIterator<Item = &'i <Self::Item as IntoIterator>::Item>, for<'i> &'i mut Self::Item: 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, for<'b> <Self::Iterable<'b> as Iterable>::Iter: DoubleEndedIterator, for<'b> Self::IterMut<'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::Item>, E1: SoM<I1>, E2: SoM<I2>,

Source§

type IterMut<'i> = Chain<<I1 as CollectionMut>::IterMut<'i>, <I2 as CollectionMut>::IterMut<'i>> where Self: 'i

Source§

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

Source§

type IterMut<'i> = Flatten<<I as CollectionMut>::IterMut<'i>> where Self: '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 Self: 'i

Source§

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

Source§

type IterMut<'i> = Rev<<I as CollectionMut>::IterMut<'i>> where Self: '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 Self: '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 Self: '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 Self: 'i

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

impl<T> CollectionMut for EmptyCol<T>

Source§

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

Source§

impl<T> CollectionMut for OnceCol<T>

Source§

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

Source§

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

Source§

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