-
Notifications
You must be signed in to change notification settings - Fork 27
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
Add a Stream::try_iter() method #70
Conversation
I'm confused about this is named _iter. Implementation has no relationship to the Iterator trait. |
I basically just took the name from libstd's |
poll_immediate and now_or_never in futures are somewhat similar to this, but I don't think they are exactly the same.
|
Unfortunately we still need a |
Oh, good point. I forgot that because now_or_never uses noop_context. |
match self.stream.poll_next(cx) { | ||
Poll::Ready(x) => Poll::Ready(x), | ||
Poll::Pending => Poll::Ready(None), | ||
} |
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.
match self.stream.poll_next(cx) { | |
Poll::Ready(x) => Poll::Ready(x), | |
Poll::Pending => Poll::Ready(None), | |
} | |
Poll::Ready(match self.stream.poll_next(cx) { | |
Poll::Ready(x) => x, | |
Poll::Pending => None, | |
}) |
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.
I don't think this looks nice
Usage of this would probably introduce unnecessary spurious wake-ups due to the lack of smth. like noop_context. |
Yes, it would be nice to have a |
Could the allocation be cached (similar to the caching in |
It could, but you would still need an atomic operation to clone it out of the thread local. While this is acceptable overhead for I don't think there's anything wrong with just passing |
1 similar comment
This comment was marked as duplicate.
This comment was marked as duplicate.
I think the concern about the name still applies here. I'd propose |
src/stream.rs
Outdated
@@ -1759,6 +1759,54 @@ pub trait StreamExt: Stream { | |||
} | |||
} | |||
|
|||
/// Runs this stream until it returns `Poll::Pending`, whereupon it yields `None`. |
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.
The stream isn't necessarily fused, right? I don't think "until" does capture what happens correctly... It might also be a good idea to provide a way to access the inner stream, in case one wants that....
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.
It might also be a good idea to provide a way to access the inner stream, in case one wants that....
I've added accessor methods. However there are many other streams in this crate that don't provide access to their underlying streams. I'm not sure if it's a good idea to add those accessor methods there as well...
Definitely out of scope for this PR though.
Also, what about pinning? How does this interact with that? |
- Rename try_iter to try_stream. - Mention non-fuse status in docs. - Add accessor methods for inner stream. Signed-off-by: John Nunley <[email protected]>
f21901f
to
ba11e59
Compare
It only takes let x = stream::once_future(async { 1 });
pin!(x);
let y = x.try_stream(); |
Error: cannot find function |
Signed-off-by: John Nunley <[email protected]>
This PR adds a
try_iter
method toStreamExt
that consumes all of the values until it getsPending
, at which point it returnsNone
. The goal is to get a method similar toReceiver::try_iter()
on theasync_channel::Receiver
type that would allow users to consume all of the values in the channel without waiting.