Skip to content

Use take_while instead of scan in impl of Product, Sum and FromStream for Option and Result #667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/option/from_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,26 @@ where
let stream = stream.into_stream();

Box::pin(async move {
// Using `scan` here because it is able to stop the stream early
// Using `take_while` here because it is able to stop the stream early
// if a failure occurs
let mut found_error = false;
let mut found_none = false;
let out: V = stream
.scan((), |_, elem| {
match elem {
Some(elem) => Some(elem),
None => {
found_error = true;
// Stop processing the stream on error
None
}
.take_while(|elem| {
elem.is_some() || {
found_none = true;
// Stop processing the stream on `None`
false
}
})
.map(Option::unwrap)
.collect()
.await;

if found_error { None } else { Some(out) }
if found_none {
None
} else {
Some(out)
}
})
}
}
26 changes: 14 additions & 12 deletions src/option/product.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::pin::Pin;

use crate::prelude::*;
use crate::stream::{Stream, Product};
use crate::stream::{Product, Stream};

impl<T, U> Product<Option<U>> for Option<T>
where
Expand Down Expand Up @@ -36,23 +36,25 @@ where
```
"#]
fn product<'a, S>(stream: S) -> Pin<Box<dyn Future<Output = Option<T>> + 'a>>
where S: Stream<Item = Option<U>> + 'a
where
S: Stream<Item = Option<U>> + 'a,
{
Box::pin(async move {
// Using `scan` here because it is able to stop the stream early
// Using `take_while` here because it is able to stop the stream early
// if a failure occurs
let mut found_none = false;
let out = <T as Product<U>>::product(stream
.scan((), |_, elem| {
match elem {
Some(elem) => Some(elem),
None => {
let out = <T as Product<U>>::product(
stream
.take_while(|elem| {
elem.is_some() || {
found_none = true;
// Stop processing the stream on error
None
// Stop processing the stream on `None`
false
}
}
})).await;
})
.map(Option::unwrap),
)
.await;

if found_none {
None
Expand Down
22 changes: 12 additions & 10 deletions src/option/sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,25 @@ where
```
"#]
fn sum<'a, S>(stream: S) -> Pin<Box<dyn Future<Output = Option<T>> + 'a>>
where S: Stream<Item = Option<U>> + 'a
where
S: Stream<Item = Option<U>> + 'a,
{
Box::pin(async move {
// Using `scan` here because it is able to stop the stream early
// Using `take_while` here because it is able to stop the stream early
// if a failure occurs
let mut found_none = false;
let out = <T as Sum<U>>::sum(stream
.scan((), |_, elem| {
match elem {
Some(elem) => Some(elem),
None => {
let out = <T as Sum<U>>::sum(
stream
.take_while(|elem| {
elem.is_some() || {
found_none = true;
// Stop processing the stream on error
None
false
}
}
})).await;
})
.map(Option::unwrap),
)
.await;

if found_none {
None
Expand Down