-
Notifications
You must be signed in to change notification settings - Fork 653
Add TryFutureExt::try_flatten_stream #1618
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
2 commits
Select commit
Hold shift + click to select a range
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,113 @@ | ||
use core::fmt; | ||
use core::pin::Pin; | ||
use futures_core::future::TryFuture; | ||
use futures_core::stream::{FusedStream, Stream, TryStream}; | ||
use futures_core::task::{Context, Poll}; | ||
use pin_utils::unsafe_pinned; | ||
|
||
/// Stream for the [`try_flatten_stream`](super::TryFutureExt::try_flatten_stream) method. | ||
#[must_use = "streams do nothing unless polled"] | ||
pub struct TryFlattenStream<Fut> | ||
where | ||
Fut: TryFuture, | ||
{ | ||
state: State<Fut, Fut::Ok>, | ||
} | ||
|
||
impl<Fut: TryFuture> TryFlattenStream<Fut> | ||
where | ||
Fut: TryFuture, | ||
Fut::Ok: TryStream<Error = Fut::Error>, | ||
{ | ||
unsafe_pinned!(state: State<Fut, Fut::Ok>); | ||
|
||
pub(super) fn new(future: Fut) -> Self { | ||
Self { | ||
state: State::Future(future) | ||
} | ||
} | ||
} | ||
|
||
impl<Fut> fmt::Debug for TryFlattenStream<Fut> | ||
where | ||
Fut: TryFuture + fmt::Debug, | ||
Fut::Ok: fmt::Debug, | ||
{ | ||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
fmt.debug_struct("TryFlattenStream") | ||
.field("state", &self.state) | ||
.finish() | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
enum State<Fut, St> { | ||
// future is not yet called or called and not ready | ||
Future(Fut), | ||
// future resolved to Stream | ||
Stream(St), | ||
// future resolved to error | ||
Done, | ||
} | ||
|
||
impl<Fut, St> State<Fut, St> { | ||
fn get_pin_mut<'a>(self: Pin<&'a mut Self>) -> State<Pin<&'a mut Fut>, Pin<&'a mut St>> { | ||
// safety: data is never moved via the resulting &mut reference | ||
match unsafe { Pin::get_unchecked_mut(self) } { | ||
// safety: the future we're re-pinning here will never be moved; | ||
// it will just be polled, then dropped in place | ||
State::Future(f) => State::Future(unsafe { Pin::new_unchecked(f) }), | ||
// safety: the stream we're repinning here will never be moved; | ||
// it will just be polled, then dropped in place | ||
State::Stream(s) => State::Stream(unsafe { Pin::new_unchecked(s) }), | ||
State::Done => State::Done, | ||
} | ||
} | ||
} | ||
|
||
impl<Fut> FusedStream for TryFlattenStream<Fut> | ||
where | ||
Fut: TryFuture, | ||
Fut::Ok: TryStream<Error = Fut::Error> + FusedStream, | ||
{ | ||
fn is_terminated(&self) -> bool { | ||
match &self.state { | ||
State::Future(_) => false, | ||
State::Stream(stream) => stream.is_terminated(), | ||
State::Done => true, | ||
} | ||
} | ||
} | ||
|
||
impl<Fut> Stream for TryFlattenStream<Fut> | ||
where | ||
Fut: TryFuture, | ||
Fut::Ok: TryStream<Error = Fut::Error>, | ||
{ | ||
type Item = Result<<Fut::Ok as TryStream>::Ok, Fut::Error>; | ||
|
||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
loop { | ||
match self.as_mut().state().get_pin_mut() { | ||
State::Future(f) => { | ||
match ready!(f.try_poll(cx)) { | ||
Ok(stream) => { | ||
// Future resolved to stream. | ||
// We do not return, but poll that | ||
// stream in the next loop iteration. | ||
self.as_mut().state().set(State::Stream(stream)); | ||
} | ||
Err(e) => { | ||
// Future resolved to error. | ||
// We have neither a pollable stream nor a future. | ||
self.as_mut().state().set(State::Done); | ||
return Poll::Ready(Some(Err(e))); | ||
} | ||
} | ||
} | ||
State::Stream(s) => return s.try_poll_next(cx), | ||
State::Done => return Poll::Ready(None), | ||
} | ||
} | ||
} | ||
} |
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
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.
'a
can be removed when rust-lang/rust#60944 is merged.