-
Notifications
You must be signed in to change notification settings - Fork 2
merge_join_by #5
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9fd2188
feat: add merge_join_by combinator
Iizuki 651c374
chore: bumb tokio from a vulnerable version
Iizuki 2f6de41
chore: edition 2021 --> 2024
Iizuki 05463ca
fix: fix some misc warnings
Iizuki d398442
docs: bump version and add changelog entry
Iizuki 27b3d9a
docs: spelling
Iizuki d5b14d3
fix: remove obsolete attribute
Iizuki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| //! How to use `StreamTools::merge_join_by()` with a `TryStream`, | ||
| //! that is, a `Stream` of `Result`s. | ||
|
|
||
| use either_or_both::EitherOrBoth::{self, Both}; | ||
| use futures::stream::{self, StreamExt, TryStreamExt}; | ||
| use std::cmp::Ordering; | ||
| use streamtools::StreamTools; | ||
|
|
||
| #[tokio::main] | ||
| async fn main() { | ||
| let left = stream::iter(vec![Ok(1), Ok(3), Err(4), Ok(5)]); | ||
| let right = stream::iter(vec![Ok(2), Ok(3), Err(3)]); | ||
|
|
||
| // Instead of using your comparison function directly, you can embed it into a closure that | ||
| // gives priority to errors. Then afterwards you can use usual try_* combinators in `futures::stream::TryStreamExt`. | ||
| let stream = left | ||
| .merge_join_by(right, |left_result, right_result| { | ||
| match (left_result, right_result) { | ||
| // Use the actual comparison function only if both are Ok. | ||
| (Ok(asset), Ok(foreign_asset)) => Ord::cmp(asset, foreign_asset), | ||
| // In the error cases return such ordering that the error(s) will be forwarded asap, | ||
| // and so we can short circuit faster. | ||
| (Err(_), Ok(_)) => Ordering::Less, | ||
| (Ok(_), Err(_)) => Ordering::Greater, | ||
| (Err(_), Err(_)) => Ordering::Equal, | ||
| } | ||
| }) | ||
| // Flip the results from inside the EitherOrs. | ||
| // EitherOrBoth has a convenient method for just this. | ||
| .map(EitherOrBoth::<Result<_, _>, Result<_, _>>::transpose); | ||
|
|
||
| // Run the stream with short circuiting. | ||
| let result: Result<Vec<_>, EitherOrBoth<_>> = stream.try_collect().await; | ||
|
|
||
| assert_eq!(result, Err(Both(4, 3))); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.