[][src]Trait resultit::flatten_results::FlattenResults

pub trait FlattenResults {
    fn flatten_results<InnerIter, Error, T>(
        self
    ) -> FlatMap<Self, Chain<Map<Flatten<IntoIter<InnerIter>>, fn(_: T) -> Result<T, Error>>, IntoIter<Result<T, Error>>>, fn(_: Result<InnerIter, Error>) -> Chain<Map<Flatten<IntoIter<InnerIter>>, fn(_: T) -> Result<T, Error>>, IntoIter<Result<T, Error>>>>
    where
        Self: Iterator<Item = Result<InnerIter, Error>> + Sized,
        InnerIter: IntoIterator<Item = T>
; }

Iterator adapter to flatten an Iterator<Item> where: Item = Result<IntoIterator,_>. This is needed because the standard flatten() adapter only works on an iterator of iterators and does not work on an iterator of results.

Example of what not to do:

// Vector of results, each of which contains its own vector of numbers.
let v: Vec<Result<Vec<i32>, MyError>> = vec![
	Ok(vec![1, 2]),
	Ok(vec![3, 4]),
	Err(MyError{}),
	Ok(vec![5, 6])
];
 
// Panics after printing the number 4.  Probably not what you want!
v.into_iter()
	.map(|res| res.unwrap())
	.flatten()
	.for_each(|i| println!("{}", i));

Instead of flatten(), use flatten_results() to flatten each Ok(IntoInterator) and pass through the errors.

// Use the FlattenResults trait to enable flatten_results() on iterators.
use resultit::FlattenResults;

// Vector of results, each of which contains its own vector of numbers.
let v: Vec<Result<Vec<i32>, MyError>> = vec![
	Ok(vec![1, 2]),
	Ok(vec![3, 4]),
	Err(MyError{}),
	Ok(vec![5, 6])
];
 
// Flatten the vector of results.
// Propagates the error instead of panicking.
let v: Vec<Result<i32, MyError>> = v.into_iter()
	.flatten_results()
	.collect();
 
println!("{:?}", v);
// [Ok(1), Ok(2), Ok(3), Ok(4), Err(MyError), Ok(5), Ok(6)]

Required methods

fn flatten_results<InnerIter, Error, T>(
    self
) -> FlatMap<Self, Chain<Map<Flatten<IntoIter<InnerIter>>, fn(_: T) -> Result<T, Error>>, IntoIter<Result<T, Error>>>, fn(_: Result<InnerIter, Error>) -> Chain<Map<Flatten<IntoIter<InnerIter>>, fn(_: T) -> Result<T, Error>>, IntoIter<Result<T, Error>>>> where
    Self: Iterator<Item = Result<InnerIter, Error>> + Sized,
    InnerIter: IntoIterator<Item = T>, 

Iterator adapter to flatten an Iterator<Item> where: Item = Result<IntoIterator,_>. See the documentation of FlattenResults.

Loading content...

Implementors

impl<It> FlattenResults for It where
    It: Iterator + Sized
[src]

Loading content...