-
-
Notifications
You must be signed in to change notification settings - Fork 3k
stream: add StreamExt::filter_map_async
#7971
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
Open
figsoda
wants to merge
6
commits into
tokio-rs:master
Choose a base branch
from
figsoda:async-closure
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+252
−2
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
91d9df3
stream: add StreamExt::filter_map_async
figsoda e82590c
stream: take into account the future for `FilterMapAsync::size_hint`
figsoda 7f8b993
stream: add a sleep to `StreamExt::filter_map_async`'s test
figsoda ab4af61
stream: fix `size_hint` for `filter_map_async`
figsoda 18dcec3
stream: add tests for `filter_map_async`
figsoda 506b1b9
stream: simplify implementation of `filter_map_async`
figsoda 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| use crate::Stream; | ||
|
|
||
| use core::fmt; | ||
| use core::future::Future; | ||
| use core::pin::Pin; | ||
| use core::task::{ready, Context, Poll}; | ||
| use pin_project_lite::pin_project; | ||
|
|
||
| pin_project! { | ||
| /// Stream for the [`filter_map_async`](super::StreamExt::filter_map_async) method. | ||
| #[must_use = "streams do nothing unless polled"] | ||
| pub struct FilterMapAsync<St, Fut, F> { | ||
| #[pin] | ||
| stream: St, | ||
| #[pin] | ||
| future: Option<Fut>, | ||
| f: F, | ||
| } | ||
| } | ||
|
|
||
| impl<St, Fut, F> fmt::Debug for FilterMapAsync<St, Fut, F> | ||
| where | ||
| St: fmt::Debug, | ||
| { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| f.debug_struct("FilterMapAsync") | ||
| .field("stream", &self.stream) | ||
| .finish() | ||
| } | ||
| } | ||
|
|
||
| impl<St, Fut, F> FilterMapAsync<St, Fut, F> { | ||
| pub(super) fn new(stream: St, f: F) -> Self { | ||
| FilterMapAsync { | ||
| stream, | ||
| future: None, | ||
| f, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<T, St, F, Fut> Stream for FilterMapAsync<St, Fut, F> | ||
| where | ||
| St: Stream, | ||
| Fut: Future<Output = Option<T>>, | ||
| F: FnMut(St::Item) -> Fut, | ||
| { | ||
| type Item = T; | ||
|
|
||
| fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<T>> { | ||
| let mut me = self.project(); | ||
|
|
||
| loop { | ||
| if let Some(future) = me.future.as_mut().as_pin_mut() { | ||
| let item = ready!(future.poll(cx)); | ||
| me.future.set(None); | ||
| if let Some(item) = item { | ||
| return Poll::Ready(Some(item)); | ||
| } | ||
| } | ||
|
|
||
| match ready!(me.stream.as_mut().poll_next(cx)) { | ||
| Some(item) => { | ||
| me.future.set(Some((me.f)(item))); | ||
| } | ||
| None => return Poll::Ready(None), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn size_hint(&self) -> (usize, Option<usize>) { | ||
| let future_len = usize::from(self.future.is_some()); | ||
| let upper = self | ||
| .stream | ||
| .size_hint() | ||
| .1 | ||
| .and_then(|upper| upper.checked_add(future_len)); | ||
| (0, upper) | ||
| } | ||
| } | ||
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,117 @@ | ||
| use futures::Stream; | ||
| use tokio::sync::Notify; | ||
| use tokio_stream::{self as stream, StreamExt}; | ||
| use tokio_test::{assert_pending, assert_ready_eq, task}; | ||
|
|
||
| mod support { | ||
| pub(crate) mod mpsc; | ||
| } | ||
|
|
||
| use support::mpsc; | ||
|
|
||
| #[tokio::test] | ||
| async fn basic() { | ||
| let (tx, rx) = mpsc::unbounded_channel_stream(); | ||
|
|
||
| let mut st = | ||
| task::spawn(rx.filter_map_async(async |x| if x % 2 == 0 { Some(x + 1) } else { None })); | ||
| assert_pending!(st.poll_next()); | ||
|
|
||
| tx.send(1).unwrap(); | ||
| assert!(st.is_woken()); | ||
| assert_pending!(st.poll_next()); | ||
|
|
||
| tx.send(2).unwrap(); | ||
| assert!(st.is_woken()); | ||
| assert_ready_eq!(st.poll_next(), Some(3)); | ||
|
|
||
| assert_pending!(st.poll_next()); | ||
|
|
||
| tx.send(3).unwrap(); | ||
| assert!(st.is_woken()); | ||
| assert_pending!(st.poll_next()); | ||
|
|
||
| drop(tx); | ||
| assert!(st.is_woken()); | ||
| assert_ready_eq!(st.poll_next(), None); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn notify_unbounded() { | ||
| let (tx, rx) = mpsc::unbounded_channel_stream(); | ||
| let notify = Notify::new(); | ||
|
|
||
| let mut st = task::spawn(rx.filter_map_async(async |x| { | ||
| notify.notified().await; | ||
| if x % 2 == 0 { | ||
| Some(x + 1) | ||
| } else { | ||
| None | ||
| } | ||
| })); | ||
| assert_pending!(st.poll_next()); | ||
|
|
||
| tx.send(0).unwrap(); | ||
| assert!(st.is_woken()); | ||
| assert_pending!(st.poll_next()); | ||
|
|
||
| notify.notify_one(); | ||
| assert!(st.is_woken()); | ||
| assert_ready_eq!(st.poll_next(), Some(1)); | ||
|
|
||
| tx.send(1).unwrap(); | ||
| assert!(!st.is_woken()); | ||
| assert_pending!(st.poll_next()); | ||
|
|
||
| notify.notify_one(); | ||
| assert!(st.is_woken()); | ||
| assert_pending!(st.poll_next()); | ||
|
|
||
| tx.send(2).unwrap(); | ||
| assert!(st.is_woken()); | ||
| assert_pending!(st.poll_next()); | ||
|
|
||
| notify.notify_one(); | ||
| assert!(st.is_woken()); | ||
| assert_ready_eq!(st.poll_next(), Some(3)); | ||
|
|
||
| drop(tx); | ||
| assert!(!st.is_woken()); | ||
| assert_ready_eq!(st.poll_next(), None); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn notify_bounded() { | ||
| let notify = Notify::new(); | ||
| let mut st = task::spawn(stream::iter(0..3).filter_map_async(async |x| { | ||
| notify.notified().await; | ||
| if x % 2 == 0 { | ||
| Some(x + 1) | ||
| } else { | ||
| None | ||
| } | ||
| })); | ||
| assert_eq!(st.size_hint(), (0, Some(3))); | ||
| assert_pending!(st.poll_next()); | ||
|
|
||
| notify.notify_one(); | ||
| assert!(st.is_woken()); | ||
| assert_eq!(st.size_hint(), (0, Some(3))); | ||
| assert_ready_eq!(st.poll_next(), Some(1)); | ||
| assert_eq!(st.size_hint(), (0, Some(2))); | ||
|
|
||
| notify.notify_one(); | ||
| assert!(!st.is_woken()); | ||
| assert_eq!(st.size_hint(), (0, Some(2))); | ||
| assert_pending!(st.poll_next()); | ||
| assert_eq!(st.size_hint(), (0, Some(1))); | ||
|
|
||
| notify.notify_one(); | ||
| assert!(st.is_woken()); | ||
| assert_eq!(st.size_hint(), (0, Some(1))); | ||
| assert_ready_eq!(st.poll_next(), Some(3)); | ||
| assert_eq!(st.size_hint(), (0, Some(0))); | ||
|
|
||
| assert!(!st.is_woken()); | ||
| assert_ready_eq!(st.poll_next(), None); | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So while we are polling the future, we do not poll the stream. It implies that something like
stream.buffer_unordered().filter_map_async(...)is really dangerous because ongoing futures inside of thebuffer_unordered()just pause out of nowhere.On the other hand, this is a pre-existing problem with
stream.then()too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would the alternative be keeping a buffer of items, so the entire stream can be polled by awaiting on one
.next()?