BoxOk

Trait BoxOk 

Source
pub trait BoxOk<T, E>: Sized {
    type Iter: Iterator<Item = Result<Box<T>, E>>;

    // Required method
    fn box_ok(self) -> Self::Iter;
}
Expand description

Represents an iterator that boxes the Ok values.

This trait is implemented for iterators over Result<T, E>, allowing them to box the Ok values using the Box<T> type.

§Implementations

Implementations of this trait must provide an implementation for the box_ok function, which returns a MapOk iterator that boxes each Ok value encountered during iteration.

§Examples

use std::num::ParseIntError;
use std::str::FromStr;
use map_ok::{BoxOk, MapOk};

struct Person {
    age: u8,
}

impl Person {
    fn new(age: u8) -> Self {
        Person { age }
    }
}

impl FromStr for Person {
    type Err = ParseIntError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let age = u8::from_str(s)?;
        Ok(Person::new(age))
    }
}

let input = vec!["10", "20", "x", "30"];
let mut iterator = input.into_iter()
    .map(Person::from_str)
    .map_ok(|p| p.age)
    .box_ok();

assert_eq!(iterator.next(), Some(Ok(Box::new(10))));
assert_eq!(iterator.next(), Some(Ok(Box::new(20))));
assert!(iterator.next().unwrap().is_err());
assert_eq!(iterator.next(), Some(Ok(Box::new(30))));
assert_eq!(iterator.next(), None);

Required Associated Types§

Source

type Iter: Iterator<Item = Result<Box<T>, E>>

Required Methods§

Source

fn box_ok(self) -> Self::Iter

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<I, T, E> BoxOk<T, E> for I
where I: Iterator<Item = Result<T, E>>,

Source§

type Iter = MapOkIter<I, T, E, Box<T>, fn(T) -> Box<T>>