Skip to content
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

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

Merged
merged 6 commits into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
25 changes: 14 additions & 11 deletions src/option/from_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::pin::Pin;

use crate::prelude::*;
use crate::stream::{FromStream, IntoStream};
use crate::utils::identity;

impl<T, V> FromStream<Option<T>> for Option<V>
where
Expand All @@ -17,24 +18,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
}
})
.filter_map(identity)
.collect()
.await;

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

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

impl<T, U> Product<Option<U>> for Option<T>
where
Expand Down Expand Up @@ -36,23 +37,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;
})
.filter_map(identity),
)
.await;

if found_none {
None
Expand Down
25 changes: 14 additions & 11 deletions src/option/sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::pin::Pin;

use crate::prelude::*;
use crate::stream::{Stream, Sum};
use crate::utils::identity;

impl<T, U> Sum<Option<U>> for Option<T>
where
Expand Down Expand Up @@ -31,23 +32,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
// Stop processing the stream on `None`
false
}
}
})).await;
})
.filter_map(identity),
)
.await;

if found_none {
None
Expand Down
2 changes: 1 addition & 1 deletion src/result/from_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ where
// if a failure occurs
let mut found_error = None;
let out: V = stream
.scan((), |_, elem| {
.scan((), |(), elem| {
match elem {
Ok(elem) => Some(elem),
Err(err) => {
Expand Down
28 changes: 15 additions & 13 deletions src/result/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, E> Product<Result<U, E>> for Result<T, E>
where
Expand Down Expand Up @@ -36,26 +36,28 @@ where
```
"#]
fn product<'a, S>(stream: S) -> Pin<Box<dyn Future<Output = Result<T, E>> + 'a>>
where S: Stream<Item = Result<U, E>> + 'a
where
S: Stream<Item = Result<U, E>> + 'a,
{
Box::pin(async move {
// Using `scan` here because it is able to stop the stream early
// if a failure occurs
let mut found_error = None;
let out = <T as Product<U>>::product(stream
.scan((), |_, elem| {
match elem {
Ok(elem) => Some(elem),
Err(err) => {
found_error = Some(err);
// Stop processing the stream on error
None
}
let out = <T as Product<U>>::product(stream.scan((), |(), elem| {
match elem {
Ok(elem) => Some(elem),
Err(err) => {
found_error = Some(err);
// Stop processing the stream on error
None
}
})).await;
}
}))
.await;

match found_error {
Some(err) => Err(err),
None => Ok(out)
None => Ok(out),
}
})
}
Expand Down
26 changes: 14 additions & 12 deletions src/result/sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,28 @@ where
```
"#]
fn sum<'a, S>(stream: S) -> Pin<Box<dyn Future<Output = Result<T, E>> + 'a>>
where S: Stream<Item = Result<U, E>> + 'a
where
S: Stream<Item = Result<U, E>> + 'a,
{
Box::pin(async move {
// Using `scan` here because it is able to stop the stream early
// if a failure occurs
let mut found_error = None;
let out = <T as Sum<U>>::sum(stream
.scan((), |_, elem| {
match elem {
Ok(elem) => Some(elem),
Err(err) => {
found_error = Some(err);
// Stop processing the stream on error
None
}
let out = <T as Sum<U>>::sum(stream.scan((), |(), elem| {
match elem {
Ok(elem) => Some(elem),
Err(err) => {
found_error = Some(err);
// Stop processing the stream on error
None
}
})).await;
}
}))
.await;

match found_error {
Some(err) => Err(err),
None => Ok(out)
None => Ok(out),
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/unit/from_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ impl FromStream<()> for () {
fn from_stream<'a, S: IntoStream<Item = ()> + 'a>(
stream: S,
) -> Pin<Box<dyn Future<Output = Self> + 'a>> {
Box::pin(stream.into_stream().for_each(|_| ()))
Box::pin(stream.into_stream().for_each(drop))
}
}
8 changes: 8 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ pub fn random(n: u32) -> u32 {
})
}

/// Returns given argument without changes.
#[allow(dead_code)]
#[doc(hidden)]
#[inline(always)]
pub(crate) fn identity<T>(arg: T) -> T {
arg
}

yoshuawuyts marked this conversation as resolved.
Show resolved Hide resolved
/// Add additional context to errors
pub(crate) trait Context {
fn context(self, message: impl Fn() -> String) -> Self;
Expand Down