Skip to content

Commit

Permalink
Rewrote Results implementation using take_while and filter_map
Browse files Browse the repository at this point in the history
  • Loading branch information
olegnn committed Jan 17, 2020
1 parent ed24801 commit 770c04d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 37 deletions.
30 changes: 19 additions & 11 deletions src/result/from_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,32 @@ where
Box::pin(async move {
// Using `scan` here because it is able to stop the stream early
// if a failure occurs
let mut is_error = false;
let mut found_error = None;
let out: V = stream
.scan(&mut found_error, |error, elem| {
match elem {
Ok(elem) => Some(elem),
Err(err) => {
**error = Some(err);
// Stop processing the stream on error
None
}
.take_while(|elem| {
// Stop processing the stream on `Err`
!is_error
&& (elem.is_ok() || {
is_error = true;
// Capture first `Err`
true
})
})
.filter_map(|elem| match elem {
Ok(value) => Some(value),
Err(err) => {
found_error = Some(err);
None
}
})
.collect()
.await;

match found_error {
Some(err) => Err(err),
None => Ok(out),
if is_error {
Err(found_error.unwrap())
} else {
Ok(out)
}
})
}
Expand Down
37 changes: 24 additions & 13 deletions src/result/product.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,33 @@ where
Box::pin(async move {
// Using `scan` here because it is able to stop the stream early
// if a failure occurs
let mut is_error = false;
let mut found_error = None;
let out = <T as Product<U>>::product(stream.scan(&mut found_error, |error, elem| {
match elem {
Ok(elem) => Some(elem),
Err(err) => {
**error = Some(err);
// Stop processing the stream on error
None
}
}
}))
let out = <T as Product<U>>::product(
stream
.take_while(|elem| {
// Stop processing the stream on `Err`
!is_error
&& (elem.is_ok() || {
is_error = true;
// Capture first `Err`
true
})
})
.filter_map(|elem| match elem {
Ok(value) => Some(value),
Err(err) => {
found_error = Some(err);
None
}
}),
)
.await;

match found_error {
Some(err) => Err(err),
None => Ok(out),
if is_error {
Err(found_error.unwrap())
} else {
Ok(out)
}
})
}
Expand Down
37 changes: 24 additions & 13 deletions src/result/sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,33 @@ where
Box::pin(async move {
// Using `scan` here because it is able to stop the stream early
// if a failure occurs
let mut is_error = false;
let mut found_error = None;
let out = <T as Sum<U>>::sum(stream.scan(&mut found_error, |error, elem| {
match elem {
Ok(elem) => Some(elem),
Err(err) => {
**error = Some(err);
// Stop processing the stream on error
None
}
}
}))
let out = <T as Sum<U>>::sum(
stream
.take_while(|elem| {
// Stop processing the stream on `Err`
!is_error
&& (elem.is_ok() || {
is_error = true;
// Capture first `Err`
true
})
})
.filter_map(|elem| match elem {
Ok(value) => Some(value),
Err(err) => {
found_error = Some(err);
None
}
}),
)
.await;

match found_error {
Some(err) => Err(err),
None => Ok(out),
if is_error {
Err(found_error.unwrap())
} else {
Ok(out)
}
})
}
Expand Down

0 comments on commit 770c04d

Please sign in to comment.