Trait map_ok::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.iter()
    .map(|s| s.parse::<Person>())
    .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

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<I, T, E> BoxOk<T, E> for I
where I: Iterator<Item = Result<T, E>>,

§

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