Trait gazebo::prelude::IterExt[][src]

pub trait IterExt {
    type Item;
    fn try_any<F, E>(self, any: F) -> Result<bool, E>
    where
        Self: Sized,
        F: FnMut(Self::Item) -> Result<bool, E>
;
fn try_all<F, E>(self, any: F) -> Result<bool, E>
    where
        Self: Sized,
        F: FnMut(Self::Item) -> Result<bool, E>
;
fn try_eq_by<I, F, E>(self, other: I, eq: F) -> Result<bool, E>
    where
        Self: Sized,
        I: IntoIterator,
        F: FnMut(Self::Item, I::Item) -> Result<bool, E>
;
fn try_cmp_by<I, F, E>(self, other: I, cmp: F) -> Result<Ordering, E>
    where
        Self: Sized,
        I: IntoIterator,
        F: FnMut(Self::Item, I::Item) -> Result<Ordering, E>
;
fn try_unzip<A, B, FromA, FromB, E>(self) -> Result<(FromA, FromB), E>
    where
        FromA: Default + Extend<A>,
        FromB: Default + Extend<B>,
        Self: Iterator<Item = Result<(A, B), E>>
;
fn into_singleton(self) -> Option<Self::Item>
    where
        Self: Sized
; }
Expand description

Extension traits on Iterator.

Associated Types

Required methods

Like any, except allow the function supplied to return a Result type, where we Err on the first encounter of Err.

use gazebo::prelude::*;

fn true_if_even_throw_on_zero(x: &usize) -> Result<bool, ()> {
    if *x == 0 {
        Err(())
    } else {
        Ok(x % 2 == 0)
    }
}

let x = [1, 3, 2];
assert_eq!(x.iter().try_any(true_if_even_throw_on_zero), Ok(true));

let x = [1, 3, 5];
assert_eq!(x.iter().try_any(true_if_even_throw_on_zero), Ok(false));

let x = [1, 0, 2];
assert_eq!(x.iter().try_any(true_if_even_throw_on_zero), Err(()));

Like all, except allow the function supplied to return a Result type, where we Err on the first encounter of Err.

use gazebo::prelude::*;

fn true_if_even_throw_on_zero(x: &usize) -> Result<bool, ()> {
    if *x == 0 {
        Err(())
    } else {
        Ok(x % 2 == 0)
    }
}

let x = [2, 4, 2];
assert_eq!(x.iter().try_all(true_if_even_throw_on_zero), Ok(true));

let x = [1, 3, 5];
assert_eq!(x.iter().try_all(true_if_even_throw_on_zero), Ok(false));

let x = [2, 0, 2];
assert_eq!(x.iter().try_all(true_if_even_throw_on_zero), Err(()));

Like eq_by, except allow the function supplied to return a Result type, where we Err on the first encounter of Err.

use gazebo::prelude::*;

fn double_eq_throw_on_zero(x: &usize, y: &usize) -> Result<bool, ()> {
    if *x == 0 || *y == 0 {
        Err(())
    } else {
        Ok(x * 2 == *y)
    }
}

let x = [1, 4, 2];
let y = [2, 8, 4];

assert_eq!(x.iter().try_eq_by(&y, double_eq_throw_on_zero), Ok(true));

let x = [1, 4, 2];
let y = [2, 0, 4];

assert_eq!(x.iter().try_eq_by(&y, double_eq_throw_on_zero), Err(()));

Like cmp_by, except allow the function supplied to return a Result type, where we Err on the first encounter of Err.

use gazebo::prelude::*;
use std::cmp::Ordering;

fn double_cmp_throw_on_zero(x: &usize, y: &usize) -> Result<Ordering, ()> {
    if *x == 0 || *y == 0 {
        Err(())
    } else {
        Ok((x * 2).cmp(y))
    }
}

let x = [1, 4, 2];
let y = [2, 8, 4];

assert_eq!(x.iter().try_cmp_by(&y, double_cmp_throw_on_zero), Ok(Ordering::Equal));

let x = [1, 2, 2];
let y = [2, 8, 4];

assert_eq!(x.iter().try_cmp_by(&y, double_cmp_throw_on_zero), Ok(Ordering::Less));

let x = [1, 4];
let y = [2, 8, 4];

assert_eq!(x.iter().try_cmp_by(&y, double_cmp_throw_on_zero), Ok(Ordering::Less));

let x = [1, 4, 4];
let y = [2, 8, 4];

assert_eq!(x.iter().try_cmp_by(&y, double_cmp_throw_on_zero), Ok(Ordering::Greater));

let x = [1, 4, 2, 3];
let y = [2, 8, 4];

assert_eq!(x.iter().try_cmp_by(&y, double_cmp_throw_on_zero), Ok(Ordering::Greater));

let x = [1, 4, 2];
let y = [2, 0, 4];

assert_eq!(x.iter().try_cmp_by(&y, double_cmp_throw_on_zero), Err(()));

Like unzip, except allowing the current Iterator to contain Result type, where we Err on the first encounter of Err.

use gazebo::prelude::*;

let i = vec![Ok::<_, ()>((1, "a")), Ok((2, "b"))];

assert_eq!(i.into_iter().try_unzip(), Ok((vec![1, 2], vec!["a", "b"])));

let i = vec![Ok((1, "a")), Err(()), Ok((2, "b"))];

assert_eq!(i.into_iter().try_unzip::<_, _, Vec<_>, Vec<_>, _>(), Err(()));

If this iterator contains a single element, return it. Otherwise, return None.

use gazebo::prelude::*;

let i = vec![1];
assert_eq!(i.into_iter().into_singleton(), Some(1));

let i = Vec::<i64>::new();
assert_eq!(i.into_iter().into_singleton(), None);

let i = vec![1, 2];
assert_eq!(i.into_iter().into_singleton(), None);

Implementors