-
Notifications
You must be signed in to change notification settings - Fork 620
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get rid of Chain as the building block for other combinators and remo…
…ve some unsafe code
- Loading branch information
Showing
5 changed files
with
63 additions
and
139 deletions.
There are no files selected for viewing
This file contains 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 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 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 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>>; |