This commit is contained in:
Sunjay Varma 2019-09-17 16:48:58 -04:00
parent ad0510110c
commit c87dab2d5e
2 changed files with 17 additions and 13 deletions

View file

@ -48,11 +48,11 @@ pub mod io;
pub mod net;
pub mod os;
pub mod prelude;
mod result;
pub mod stream;
pub mod sync;
pub mod task;
mod vec;
mod result;
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
#[cfg(feature = "unstable")]

View file

@ -3,7 +3,9 @@ use crate::stream::{FromStream, IntoStream, Stream};
use std::pin::Pin;
impl<T: Send, E: Send, V> FromStream<Result<T, E>> for Result<V, E>
where V: FromStream<T> {
where
V: FromStream<T>,
{
/// Takes each element in the stream: if it is an `Err`, no further
/// elements are taken, and the `Err` is returned. Should no `Err`
/// occur, a container with the values of each `Result` is returned.
@ -22,16 +24,19 @@ impl<T: Send, E: Send, V> FromStream<Result<T, E>> for Result<V, E>
// Using `scan` here because it is able to stop the stream early
// if a failure occurs
let mut found_error = None;
let out: V = stream.scan((), |_, elem| {
match elem {
Ok(elem) => Some(elem),
Err(err) => {
found_error = Some(err);
// Stop processing the stream on error
None
},
}
}).collect().await;
let out: V = stream
.scan((), |_, elem| {
match elem {
Ok(elem) => Some(elem),
Err(err) => {
found_error = Some(err);
// Stop processing the stream on error
None
}
}
})
.collect()
.await;
match found_error {
Some(err) => Err(err),
@ -40,4 +45,3 @@ impl<T: Send, E: Send, V> FromStream<Result<T, E>> for Result<V, E>
}))
}
}