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>
; }
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(()));

Implementors