Trait CollectionMutObj

Source
pub trait CollectionMutObj: CollectionObj {
    // Required method
    fn boxed_iter_mut(
        &mut self,
    ) -> Box<dyn Iterator<Item = &mut Self::Item> + '_>;
}
Expand description

In addition to boxed_iter, a CollectionMutObj provides the boxed_iter_mut method which returns a boxed iterator over mutable references of elements of the collection.

It is the object safe counterpart of CollectionMut trait which can conveniently be made into a trait object.

Note that for collections, CollectionMutObj is implicitly implemented and readily available. Please refer to CollectionMut documentation for details of automatic implementations.

In order to use object safe iterables and collections please add --features std and use use orx_iterable::{*, obj_safe::*} to import dependencies rather than use orx_iterable::*.

§Examples

use orx_iterable::obj_safe::*;
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 dyn CollectionMutObj<Item = i32>) {
    let sum: i32 = numbers.boxed_iter().sum();

    for x in numbers.boxed_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 Methods§

Source

fn boxed_iter_mut(&mut self) -> Box<dyn Iterator<Item = &mut Self::Item> + '_>

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

Implementors§

Source§

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

Source§

impl<I, E> CollectionMutObj 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§

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

Source§

impl<I, E> CollectionMutObj 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§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

impl<T> CollectionMutObj for EmptyCol<T>

Source§

impl<T> CollectionMutObj for OnceCol<T>

Source§

impl<X> CollectionMutObj 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>,