Trait wagon_utils::ResultNext
source · pub trait ResultNext<T, E>: Iterator<Item = Result<T, E>> {
// Required method
fn next_result(&mut self) -> Result<T, E>;
}
Expand description
Quickly get a result from an iterator of Result
s.
This trait is automatically implemented for any iterator of Result
s that has an error type
that implements From<UnexpectedEnd>
.
Required Methods§
sourcefn next_result(&mut self) -> Result<T, E>
fn next_result(&mut self) -> Result<T, E>
If you have an iterator that holds Result
items, you start having to deal with nested Some(Ok(...))
patterns,
which gets annoying quickly. This trait is intended so that the iterator always returns some sort of Result
, which can then be unwrapped as needed (probably using ?
).
§Example
struct IterVec<T, E>(Vec<Result<T, E>>);
#[derive(Debug, Eq, PartialEq)]
enum IterErr {
SomeErr,
EndErr
}
impl From<UnexpectedEnd> for IterErr {
}
impl<T, E> Iterator for IterVec<T, E> {
}
let mut iter: IterVec<i32, IterErr> = IterVec(vec![Ok(1)]);
assert_eq!(Ok(1), iter.next_result());
assert_eq!(Err(IterErr::EndErr), iter.next_result());
§Errors
Should return Err
if the underlying item is an Err
, or there are no more items in the iterator.