Trait SliceTools

Source
pub trait SliceTools {
    // Provided methods
    fn cartesian_product_mut<'a, S, T: 'a, U: 'a>(
        &'a mut self,
        slice: &'a mut S,
    ) -> CartesianProductMut<'a, T, U>
       where Self: AsMut<[T]>,
             S: AsMut<[U]> { ... }
    fn pairs_mut<'a, T: 'a>(&'a mut self) -> PairsMut<'a, T>
       where Self: AsMut<[T]> { ... }
    fn group_mut<'a, T>(&'a mut self) -> GroupMut<'a, T>
       where Self: AsMut<[T]>,
             T: PartialEq + 'a { ... }
    fn group_by_key_mut<'a, T: 'a, K, F>(
        &'a mut self,
        condition: F,
    ) -> GroupByKeyMut<'a, T, K, F>
       where Self: AsMut<[T]>,
             K: PartialEq,
             F: Fn(&T) -> K { ... }
    fn group_by_mut<'a, T, F>(
        &'a mut self,
        condition: F,
    ) -> GroupByMut<'a, T, F>
       where Self: AsMut<[T]>,
             T: PartialEq + 'a,
             F: Fn(&T, &T) -> bool { ... }
}
Expand description

A trait to extend slices and Vecs with streaming iterators.

Provided Methods§

Source

fn cartesian_product_mut<'a, S, T: 'a, U: 'a>( &'a mut self, slice: &'a mut S, ) -> CartesianProductMut<'a, T, U>
where Self: AsMut<[T]>, S: AsMut<[U]>,

Iterate on all the pairs (a, b) from slices A and B such as a ∈ A and b ∈ B, yielding mutables references on items.

§Example
use slicetools::*;

let mut v1 = vec![100; 3];
let mut v2 = vec![1, 2, 3, 4];
{
    let mut it = v1.cartesian_product_mut(&mut v2);
     
    while let Some((a, b)) = it.next() {
        *a += *b;
    }
}
assert_eq!(v1, &[110; 3]);
Source

fn pairs_mut<'a, T: 'a>(&'a mut self) -> PairsMut<'a, T>
where Self: AsMut<[T]>,

Iterate on all the possible pairs of the slice, yielding mutables references on items.

§Example
use slicetools::*;

let mut v = vec![1, 2, 3, 4];
{
    let mut it = v.pairs_mut();
     
    while let Some((a, b)) = it.next() {
        if *b > *a {
            *a += 1;
        }
    }
}
assert_eq!(v, &[4, 4, 4, 4]);
Source

fn group_mut<'a, T>(&'a mut self) -> GroupMut<'a, T>
where Self: AsMut<[T]>, T: PartialEq + 'a,

Iterate on groups of equal items, yielding mutables references on slices.

§Example
use slicetools::*;

let mut v = vec![100, 100, 100, 200, 200, 200, 200];
{
    let mut it = v.group_mut();
     
    while let Some(slice) = it.next() {
        for (i, num) in slice.iter_mut().enumerate() {
            *num += i;
        }
    }
}
assert_eq!(v, &[100, 101, 102, 200, 201, 202, 203]);
Source

fn group_by_key_mut<'a, T: 'a, K, F>( &'a mut self, condition: F, ) -> GroupByKeyMut<'a, T, K, F>
where Self: AsMut<[T]>, K: PartialEq, F: Fn(&T) -> K,

Iterate on groups of equal items, yielding mutables references on slices.

§Example
use slicetools::*;

let mut v = vec![(0, 10), (0, 10), (1, 10), (1, 10), (1, 10)];
{
    let mut it = v.group_by_key_mut(|a| a.0);
     
    while let Some(slice) = it.next() {
        for (i, pair) in slice.iter_mut().enumerate() {
            pair.1 += i as i32;
        }
    }
}
assert_eq!(v, &[(0, 10), (0, 11), (1, 10), (1, 11), (1, 12)]);
Source

fn group_by_mut<'a, T, F>(&'a mut self, condition: F) -> GroupByMut<'a, T, F>
where Self: AsMut<[T]>, T: PartialEq + 'a, F: Fn(&T, &T) -> bool,

Iterate on groups of equal items, yielding mutables references on slices.

§Example
use slicetools::*;

let mut v = vec![0_i32, -1, -2, 2, 2, 2];
{
    let mut it = v.group_by_mut(|&a, &b| b.signum() == a.signum());
     
    while let Some(slice) = it.next() {
        for (i, num) in slice.iter_mut().enumerate() {
            *num += i as i32;
        }
    }
}
assert_eq!(v, &[0, -1, -1, 2, 3, 4]);

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<T> SliceTools for T