-
Notifications
You must be signed in to change notification settings - Fork 651
Get rid of Chain as the building block for other combinators and remo… #2117
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
Closed
Closed
Changes from all commits
Commits
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,56 +1,84 @@ | ||
use super::chain::Chain; | ||
use core::fmt; | ||
use core::fmt::{self, Debug}; | ||
use core::pin::Pin; | ||
use futures_core::future::{FusedFuture, Future}; | ||
use futures_core::task::{Context, Poll}; | ||
use pin_utils::unsafe_pinned; | ||
use pin_project::pin_project; | ||
|
||
/// Future for the [`flatten`](super::FutureExt::flatten) method. | ||
#[must_use = "futures do nothing unless you `.await` or poll them"] | ||
pub struct Flatten<Fut> | ||
#[pin_project] | ||
#[derive(Debug)] | ||
enum InternalFlatten<Fut: Future> { | ||
First(#[pin] Fut), | ||
Second(#[pin] Fut::Output), | ||
Empty, | ||
} | ||
|
||
impl<Fut: Future> InternalFlatten<Fut> { | ||
fn new(future: Fut) -> Self { | ||
InternalFlatten::First(future) | ||
} | ||
} | ||
|
||
impl<Fut> FusedFuture for InternalFlatten<Fut> | ||
where Fut: Future, | ||
Fut::Output: Future, | ||
{ | ||
state: Chain<Fut, Fut::Output, ()>, | ||
fn is_terminated(&self) -> bool { | ||
match self { | ||
InternalFlatten::Empty => true, | ||
_ => false, | ||
} | ||
} | ||
} | ||
|
||
impl<Fut> Flatten<Fut> | ||
impl<Fut> Future for InternalFlatten<Fut> | ||
where Fut: Future, | ||
Fut::Output: Future, | ||
{ | ||
unsafe_pinned!(state: Chain<Fut, Fut::Output, ()>); | ||
type Output = <Fut::Output as Future>::Output; | ||
|
||
pub(super) fn new(future: Fut) -> Flatten<Fut> { | ||
Flatten { | ||
state: Chain::new(future, ()), | ||
} | ||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
Poll::Ready(loop { | ||
match self.as_mut().project() { | ||
__InternalFlattenProjection::First(f) => { | ||
let f = ready!(f.poll(cx)); | ||
self.set(InternalFlatten::Second(f)); | ||
}, | ||
__InternalFlattenProjection::Second(f) => { | ||
let output = ready!(f.poll(cx)); | ||
self.set(InternalFlatten::Empty); | ||
break output; | ||
}, | ||
__InternalFlattenProjection::Empty => unreachable!() | ||
} | ||
}) | ||
} | ||
} | ||
|
||
impl<Fut> fmt::Debug for Flatten<Fut> | ||
where Fut: Future + fmt::Debug, | ||
Fut::Output: fmt::Debug, | ||
{ | ||
/// Future for the [`flatten`](super::FutureExt::flatten) method. | ||
#[must_use = "futures do nothing unless you `.await` or poll them"] | ||
#[pin_project] | ||
pub struct Flatten<Fut: Future>(#[pin] InternalFlatten<Fut>); | ||
|
||
impl<Fut: Debug + Future> Debug for Flatten<Fut> where Fut::Output: Debug { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_struct("Flatten") | ||
.field("state", &self.state) | ||
.finish() | ||
self.0.fmt(f) | ||
} | ||
} | ||
|
||
impl<Fut> FusedFuture for Flatten<Fut> | ||
where Fut: Future, | ||
Fut::Output: Future, | ||
{ | ||
fn is_terminated(&self) -> bool { self.state.is_terminated() } | ||
impl<Fut: Future> Flatten<Fut> { | ||
pub(super) fn new(future: Fut) -> Self { | ||
Self(InternalFlatten::new(future)) | ||
} | ||
} | ||
|
||
impl<Fut> Future for Flatten<Fut> | ||
where Fut: Future, | ||
Fut::Output: Future, | ||
{ | ||
impl<Fut: Future> FusedFuture for Flatten<Fut> where Fut::Output: Future { | ||
fn is_terminated(&self) -> bool { self.0.is_terminated() } | ||
} | ||
|
||
impl<Fut: Future> Future for Flatten<Fut> where Fut::Output: Future { | ||
type Output = <Fut::Output as Future>::Output; | ||
|
||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
self.state().poll(cx, |a, ()| a) | ||
self.project().0.poll(cx) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,46 +1,4 @@ | ||
use super::Chain; | ||
use core::pin::Pin; | ||
use futures_core::future::{FusedFuture, Future}; | ||
use futures_core::task::{Context, Poll}; | ||
use pin_utils::unsafe_pinned; | ||
use super::{Map, Flatten}; | ||
|
||
/// Future for the [`then`](super::FutureExt::then) method. | ||
#[derive(Debug)] | ||
#[must_use = "futures do nothing unless you `.await` or poll them"] | ||
pub struct Then<Fut1, Fut2, F> { | ||
chain: Chain<Fut1, Fut2, F>, | ||
} | ||
|
||
impl<Fut1, Fut2, F> Then<Fut1, Fut2, F> | ||
where Fut1: Future, | ||
Fut2: Future, | ||
{ | ||
unsafe_pinned!(chain: Chain<Fut1, Fut2, F>); | ||
|
||
/// Creates a new `Then`. | ||
pub(super) fn new(future: Fut1, f: F) -> Then<Fut1, Fut2, F> { | ||
Then { | ||
chain: Chain::new(future, f), | ||
} | ||
} | ||
} | ||
|
||
impl<Fut1, Fut2, F> FusedFuture for Then<Fut1, Fut2, F> | ||
where Fut1: Future, | ||
Fut2: Future, | ||
F: FnOnce(Fut1::Output) -> Fut2, | ||
{ | ||
fn is_terminated(&self) -> bool { self.chain.is_terminated() } | ||
} | ||
|
||
impl<Fut1, Fut2, F> Future for Then<Fut1, Fut2, F> | ||
where Fut1: Future, | ||
Fut2: Future, | ||
F: FnOnce(Fut1::Output) -> Fut2, | ||
{ | ||
type Output = Fut2::Output; | ||
|
||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Fut2::Output> { | ||
self.as_mut().chain().poll(cx, |output, f| f(output)) | ||
} | ||
} | ||
pub type Then<Fut1, F> = Flatten<Map<Fut1, F>>; |
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.
This is an internal type of the generated code which shouldn't be used. You can use the
#[project]
attribute to allow matching on the projected variants (see https://docs.rs/pin-project/0.4.8/pin_project/attr.pin_project.html#enums)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.
Yeah, I've fixed that in the larger PR I have :)