Skip to content

Commit 8a268d5

Browse files
Merge pull request #1106 from MajorBreakfast/try-refactor
Refactor `TryFuture` combinators
2 parents f388c3e + 3150752 commit 8a268d5

File tree

13 files changed

+131
-111
lines changed

13 files changed

+131
-111
lines changed

futures-util/src/try_future/and_then.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ impl<Fut1, Fut2, F> AndThen<Fut1, Fut2, F>
2020
unsafe_pinned!(try_chain: TryChain<Fut1, Fut2, F>);
2121

2222
/// Creates a new `Then`.
23-
pub(super) fn new(future: Fut1, async_op: F) -> AndThen<Fut1, Fut2, F> {
23+
pub(super) fn new(future: Fut1, f: F) -> AndThen<Fut1, Fut2, F> {
2424
AndThen {
25-
try_chain: TryChain::new(future, async_op),
25+
try_chain: TryChain::new(future, f),
2626
}
2727
}
2828
}

futures-util/src/try_future/err_into.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,32 @@ use futures_core::task::{self, Poll};
88
/// This is created by the `Future::err_into` method.
99
#[derive(Debug)]
1010
#[must_use = "futures do nothing unless polled"]
11-
pub struct ErrInto<A, E> {
12-
future: A,
11+
pub struct ErrInto<Fut, E> {
12+
future: Fut,
1313
_marker: PhantomData<E>,
1414
}
1515

16-
impl<A, E> ErrInto<A, E> {
17-
unsafe_pinned!(future: A);
18-
}
16+
impl<Fut, E> ErrInto<Fut, E> {
17+
unsafe_pinned!(future: Fut);
1918

20-
pub fn new<A, E>(future: A) -> ErrInto<A, E> {
21-
ErrInto {
22-
future,
23-
_marker: PhantomData,
19+
pub(super) fn new(future: Fut) -> ErrInto<Fut, E> {
20+
ErrInto {
21+
future,
22+
_marker: PhantomData,
23+
}
2424
}
2525
}
2626

27-
impl<A, E> Future for ErrInto<A, E>
28-
where A: TryFuture,
29-
A::Error: Into<E>,
27+
impl<Fut, E> Future for ErrInto<Fut, E>
28+
where Fut: TryFuture,
29+
Fut::Error: Into<E>,
3030
{
31-
type Output = Result<A::Ok, E>;
31+
type Output = Result<Fut::Ok, E>;
3232

33-
fn poll(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<Self::Output> {
33+
fn poll(
34+
mut self: PinMut<Self>,
35+
cx: &mut task::Context,
36+
) -> Poll<Self::Output> {
3437
match self.future().try_poll(cx) {
3538
Poll::Pending => Poll::Pending,
3639
Poll::Ready(output) => {

futures-util/src/try_future/flatten_sink.rs

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use futures_core::task::{self, Poll};
55
use futures_sink::Sink;
66

77
#[derive(Debug)]
8-
enum State<F, S> {
9-
Waiting(F),
10-
Ready(S),
8+
enum State<Fut, Si> {
9+
Waiting(Fut),
10+
Ready(Si),
1111
Closed,
1212
}
1313
use self::State::*;
@@ -17,14 +17,22 @@ use self::State::*;
1717
///
1818
/// This is created by the `Future::flatten_sink` method.
1919
#[derive(Debug)]
20-
pub struct FlattenSink<F, S>(State<F, S>);
20+
pub struct FlattenSink<Fut, Si>(State<Fut, Si>);
2121

22-
impl<F: Unpin, S: Unpin> Unpin for FlattenSink<F, S> {}
22+
impl<Fut: Unpin, Si: Unpin> Unpin for FlattenSink<Fut, Si> {}
23+
24+
impl<Fut, Si> FlattenSink<Fut, Si>
25+
where
26+
Fut: TryFuture<Ok = Si>,
27+
Si: Sink<SinkError = Fut::Error>,
28+
{
29+
pub(super) fn new(future: Fut) -> FlattenSink<Fut, Si> {
30+
FlattenSink(Waiting(future))
31+
}
2332

24-
impl<F, S> FlattenSink<F, S> {
2533
fn project_pin<'a>(
2634
self: PinMut<'a, Self>
27-
) -> State<PinMut<'a, F>, PinMut<'a, S>> {
35+
) -> State<PinMut<'a, Fut>, PinMut<'a, Si>> {
2836
unsafe {
2937
match &mut PinMut::get_mut_unchecked(self).0 {
3038
Waiting(f) => Waiting(PinMut::new_unchecked(f)),
@@ -35,17 +43,18 @@ impl<F, S> FlattenSink<F, S> {
3543
}
3644
}
3745

38-
impl<F, S> Sink for FlattenSink<F, S>
46+
impl<Fut, Si> Sink for FlattenSink<Fut, Si>
3947
where
40-
F: TryFuture<Ok = S>,
41-
S: Sink<SinkError = F::Error>,
48+
Fut: TryFuture<Ok = Si>,
49+
Si: Sink<SinkError = Fut::Error>,
4250
{
43-
type SinkItem = S::SinkItem;
44-
type SinkError = S::SinkError;
51+
type SinkItem = Si::SinkItem;
52+
type SinkError = Si::SinkError;
4553

46-
fn poll_ready(mut self: PinMut<Self>, cx: &mut task::Context)
47-
-> Poll<Result<(), Self::SinkError>>
48-
{
54+
fn poll_ready(
55+
mut self: PinMut<Self>,
56+
cx: &mut task::Context,
57+
) -> Poll<Result<(), Self::SinkError>> {
4958
let resolved_stream = match self.reborrow().project_pin() {
5059
Ready(s) => return s.poll_ready(cx),
5160
Waiting(f) => try_ready!(f.try_poll(cx)),
@@ -59,19 +68,21 @@ where
5968
}
6069
}
6170

62-
fn start_send(self: PinMut<Self>, item: Self::SinkItem)
63-
-> Result<(), Self::SinkError>
64-
{
71+
fn start_send(
72+
self: PinMut<Self>,
73+
item: Self::SinkItem,
74+
) -> Result<(), Self::SinkError> {
6575
match self.project_pin() {
6676
Ready(s) => s.start_send(item),
6777
Waiting(_) => panic!("poll_ready not called first"),
6878
Closed => panic!("start_send called after eof"),
6979
}
7080
}
7181

72-
fn poll_flush(self: PinMut<Self>, cx: &mut task::Context)
73-
-> Poll<Result<(), Self::SinkError>>
74-
{
82+
fn poll_flush(
83+
self: PinMut<Self>,
84+
cx: &mut task::Context,
85+
) -> Poll<Result<(), Self::SinkError>> {
7586
match self.project_pin() {
7687
Ready(s) => s.poll_flush(cx),
7788
// if sink not yet resolved, nothing written ==> everything flushed
@@ -80,9 +91,10 @@ where
8091
}
8192
}
8293

83-
fn poll_close(mut self: PinMut<Self>, cx: &mut task::Context)
84-
-> Poll<Result<(), Self::SinkError>>
85-
{
94+
fn poll_close(
95+
mut self: PinMut<Self>,
96+
cx: &mut task::Context,
97+
) -> Poll<Result<(), Self::SinkError>> {
8698
let res = match self.reborrow().project_pin() {
8799
Ready(s) => s.poll_close(cx),
88100
Waiting(_) | Closed => Poll::Ready(Ok(())),
@@ -93,11 +105,3 @@ where
93105
res
94106
}
95107
}
96-
97-
pub fn new<F, S>(fut: F) -> FlattenSink<F, S>
98-
where
99-
F: TryFuture<Ok = S>,
100-
S: Sink<SinkError = F::Error>,
101-
{
102-
FlattenSink(Waiting(fut))
103-
}

futures-util/src/try_future/into_future.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ impl<Fut: TryFuture> Future for IntoFuture<Fut> {
2222
type Output = Result<Fut::Ok, Fut::Error>;
2323

2424
#[inline]
25-
fn poll(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<Self::Output> {
25+
fn poll(
26+
mut self: PinMut<Self>,
27+
cx: &mut task::Context,
28+
) -> Poll<Self::Output> {
2629
self.future().try_poll(cx)
2730
}
2831
}

futures-util/src/try_future/map_err.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ use futures_core::task::{self, Poll};
1010
#[must_use = "futures do nothing unless polled"]
1111
pub struct MapErr<Fut, F> {
1212
future: Fut,
13-
op: Option<F>,
13+
f: Option<F>,
1414
}
1515

1616
impl<Fut, F> MapErr<Fut, F> {
1717
unsafe_pinned!(future: Fut);
18-
unsafe_unpinned!(op: Option<F>);
18+
unsafe_unpinned!(f: Option<F>);
1919

2020
/// Creates a new MapErr.
21-
pub(super) fn new(future: Fut, op: F) -> MapErr<Fut, F> {
22-
MapErr { future, op: Some(op) }
21+
pub(super) fn new(future: Fut, f: F) -> MapErr<Fut, F> {
22+
MapErr { future, f: Some(f) }
2323
}
2424
}
2525

@@ -31,13 +31,16 @@ impl<Fut, F, E> Future for MapErr<Fut, F>
3131
{
3232
type Output = Result<Fut::Ok, E>;
3333

34-
fn poll(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<Self::Output> {
34+
fn poll(
35+
mut self: PinMut<Self>,
36+
cx: &mut task::Context,
37+
) -> Poll<Self::Output> {
3538
match self.future().try_poll(cx) {
3639
Poll::Pending => Poll::Pending,
3740
Poll::Ready(result) => {
38-
let op = self.op().take()
41+
let f = self.f().take()
3942
.expect("MapErr must not be polled after it returned `Poll::Ready`");
40-
Poll::Ready(result.map_err(op))
43+
Poll::Ready(result.map_err(f))
4144
}
4245
}
4346
}

futures-util/src/try_future/map_ok.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ use futures_core::task::{self, Poll};
1010
#[must_use = "futures do nothing unless polled"]
1111
pub struct MapOk<Fut, F> {
1212
future: Fut,
13-
op: Option<F>,
13+
f: Option<F>,
1414
}
1515

1616
impl<Fut, F> MapOk<Fut, F> {
1717
unsafe_pinned!(future: Fut);
18-
unsafe_unpinned!(op: Option<F>);
18+
unsafe_unpinned!(f: Option<F>);
1919

2020
/// Creates a new MapOk.
21-
pub(super) fn new(future: Fut, op: F) -> MapOk<Fut, F> {
22-
MapOk { future, op: Some(op) }
21+
pub(super) fn new(future: Fut, f: F) -> MapOk<Fut, F> {
22+
MapOk { future, f: Some(f) }
2323
}
2424
}
2525

@@ -31,11 +31,14 @@ impl<Fut, F, T> Future for MapOk<Fut, F>
3131
{
3232
type Output = Result<T, Fut::Error>;
3333

34-
fn poll(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<Self::Output> {
34+
fn poll(
35+
mut self: PinMut<Self>,
36+
cx: &mut task::Context,
37+
) -> Poll<Self::Output> {
3538
match self.future().try_poll(cx) {
3639
Poll::Pending => Poll::Pending,
3740
Poll::Ready(result) => {
38-
let op = self.op().take()
41+
let op = self.f().take()
3942
.expect("MapOk must not be polled after it returned `Poll::Ready`");
4043
Poll::Ready(result.map(op))
4144
}

futures-util/src/try_future/mod.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ pub trait TryFutureExt: TryFuture {
6666
/// version of it.
6767
fn flatten_sink(self) -> FlattenSink<Self, Self::Ok>
6868
where
69-
Self::Ok: Sink<SinkError=Self::Error>,
69+
Self::Ok: Sink<SinkError = Self::Error>,
7070
Self: Sized,
7171
{
72-
flatten_sink::new(self)
72+
FlattenSink::new(self)
7373
}
7474

7575
/// Map this future's result to a different type, returning a new future of
@@ -112,11 +112,11 @@ pub trait TryFutureExt: TryFuture {
112112
/// let new_future = future.map_ok(|x| x + 3);
113113
/// assert_eq!(block_on(new_future), Err(1));
114114
/// ```
115-
fn map_ok<T, F>(self, op: F) -> MapOk<Self, F>
115+
fn map_ok<T, F>(self, f: F) -> MapOk<Self, F>
116116
where F: FnOnce(Self::Ok) -> T,
117117
Self: Sized,
118118
{
119-
MapOk::new(self, op)
119+
MapOk::new(self, f)
120120
}
121121

122122
/// Map this future's error to a different error, returning a new future.
@@ -158,11 +158,11 @@ pub trait TryFutureExt: TryFuture {
158158
/// let new_future = future.map_err(|x| x + 3);
159159
/// assert_eq!(block_on(new_future), Ok(1));
160160
/// ```
161-
fn map_err<E, F>(self, op: F) -> MapErr<Self, F>
161+
fn map_err<E, F>(self, f: F) -> MapErr<Self, F>
162162
where F: FnOnce(Self::Error) -> E,
163163
Self: Sized,
164164
{
165-
MapErr::new(self, op)
165+
MapErr::new(self, f)
166166
}
167167

168168
/// Map this future's error to a new error type using the `Into` trait.
@@ -190,7 +190,7 @@ pub trait TryFutureExt: TryFuture {
190190
where Self: Sized,
191191
Self::Error: Into<E>
192192
{
193-
err_into::new(self)
193+
ErrInto::new(self)
194194
}
195195

196196
/// Execute another future after this one has resolved successfully.
@@ -227,12 +227,12 @@ pub trait TryFutureExt: TryFuture {
227227
/// panic!("should not be called in case of an error");
228228
/// });
229229
/// ```
230-
fn and_then<Fut, F>(self, async_op: F) -> AndThen<Self, Fut, F>
230+
fn and_then<Fut, F>(self, f: F) -> AndThen<Self, Fut, F>
231231
where F: FnOnce(Self::Ok) -> Fut,
232232
Fut: TryFuture<Error = Self::Error>,
233233
Self: Sized,
234234
{
235-
AndThen::new(self, async_op)
235+
AndThen::new(self, f)
236236
}
237237

238238
/// Execute another future if this one resolves with an error.
@@ -269,12 +269,12 @@ pub trait TryFutureExt: TryFuture {
269269
/// panic!("should not be called in case of success");
270270
/// });
271271
/// ```
272-
fn or_else<Fut, F>(self, async_op: F) -> OrElse<Self, Fut, F>
272+
fn or_else<Fut, F>(self, f: F) -> OrElse<Self, Fut, F>
273273
where F: FnOnce(Self::Error) -> Fut,
274274
Fut: TryFuture<Ok = Self::Ok>,
275275
Self: Sized,
276276
{
277-
OrElse::new(self, async_op)
277+
OrElse::new(self, f)
278278
}
279279

280280
/* TODO
@@ -427,11 +427,11 @@ pub trait TryFutureExt: TryFuture {
427427
/// let new_future = future.unwrap_or_else(|_| ());
428428
/// assert_eq!(block_on(new_future), ());
429429
/// ```
430-
fn unwrap_or_else<F>(self, op: F) -> UnwrapOrElse<Self, F>
430+
fn unwrap_or_else<F>(self, f: F) -> UnwrapOrElse<Self, F>
431431
where Self: Sized,
432432
F: FnOnce(Self::Error) -> Self::Ok
433433
{
434-
UnwrapOrElse::new(self, op)
434+
UnwrapOrElse::new(self, f)
435435
}
436436

437437
/// Wraps a `TryFuture` so that it implements `Future`. `TryFuture`s

futures-util/src/try_future/or_else.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ impl<Fut1, Fut2, F> OrElse<Fut1, Fut2, F>
2020
unsafe_pinned!(try_chain: TryChain<Fut1, Fut2, F>);
2121

2222
/// Creates a new `Then`.
23-
pub(super) fn new(future: Fut1, async_op: F) -> OrElse<Fut1, Fut2, F> {
23+
pub(super) fn new(future: Fut1, f: F) -> OrElse<Fut1, Fut2, F> {
2424
OrElse {
25-
try_chain: TryChain::new(future, async_op),
25+
try_chain: TryChain::new(future, f),
2626
}
2727
}
2828
}
@@ -34,7 +34,10 @@ impl<Fut1, Fut2, F> Future for OrElse<Fut1, Fut2, F>
3434
{
3535
type Output = Result<Fut2::Ok, Fut2::Error>;
3636

37-
fn poll(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<Self::Output> {
37+
fn poll(
38+
mut self: PinMut<Self>,
39+
cx: &mut task::Context,
40+
) -> Poll<Self::Output> {
3841
self.try_chain().poll(cx, |result, async_op| {
3942
match result {
4043
Ok(ok) => TryChainAction::Output(Ok(ok)),

0 commit comments

Comments
 (0)