1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//! Compatible layer of asynchronous tasks used within the framework.

use crate::{error::Error, input::Input, util::Either};

#[doc(no_inline)]
pub use futures01::{try_ready, Async, Poll};

/// A trait that abstracts the general asynchronous tasks within the framework.
pub trait TryFuture {
    type Ok;
    type Error: Into<Error>;

    fn poll_ready(&mut self, input: &mut Input<'_>) -> Poll<Self::Ok, Self::Error>;
}

impl<F> TryFuture for Box<F>
where
    F: TryFuture + ?Sized,
{
    type Ok = F::Ok;
    type Error = F::Error;

    #[inline]
    fn poll_ready(&mut self, input: &mut Input<'_>) -> Poll<Self::Ok, Self::Error> {
        (**self).poll_ready(input)
    }
}

impl<L, R> TryFuture for Either<L, R>
where
    L: TryFuture,
    R: TryFuture,
{
    type Ok = Either<L::Ok, R::Ok>;
    type Error = Error;

    #[inline]
    fn poll_ready(&mut self, input: &mut Input<'_>) -> Poll<Self::Ok, Self::Error> {
        match self {
            Either::Left(l) => l
                .poll_ready(input)
                .map(|x| x.map(Either::Left))
                .map_err(Into::into),
            Either::Right(r) => r
                .poll_ready(input)
                .map(|x| x.map(Either::Right))
                .map_err(Into::into),
        }
    }
}

pub fn poll_fn<T, E>(
    op: impl FnMut(&mut Input<'_>) -> Poll<T, E>,
) -> impl TryFuture<
    Ok = T, //
    Error = E,
>
where
    E: Into<Error>,
{
    #[allow(missing_debug_implementations)]
    struct PollFn<F>(F);

    impl<F, T, E> TryFuture for PollFn<F>
    where
        F: FnMut(&mut Input<'_>) -> Poll<T, E>,
        E: Into<Error>,
    {
        type Ok = T;
        type Error = E;

        #[inline]
        fn poll_ready(&mut self, input: &mut Input<'_>) -> Poll<Self::Ok, Self::Error> {
            (self.0)(input)
        }
    }

    PollFn(op)
}

pub fn oneshot<T, E>(
    f: impl FnOnce(&mut Input<'_>) -> Result<T, E>,
) -> impl TryFuture<Ok = T, Error = E>
where
    E: Into<Error>,
{
    let mut f = Some(f);
    self::poll_fn(move |input| (f.take().unwrap())(input).map(Into::into))
}

/// A wrapper struct that provides the implementation of `TryFuture` for
/// implementors of futures 0.1 `Future`.
#[derive(Debug)]
#[must_use = "futures do nothing unless polled."]
pub struct Compat01<F>(F);

impl<F> From<F> for Compat01<F>
where
    F: futures01::Future,
    F::Error: Into<Error>,
{
    fn from(future: F) -> Self {
        Compat01(future)
    }
}

impl<F> TryFuture for Compat01<F>
where
    F: futures01::Future,
    F::Error: Into<Error>,
{
    type Ok = F::Item;
    type Error = F::Error;

    #[inline]
    fn poll_ready(&mut self, _: &mut Input<'_>) -> Poll<Self::Ok, Self::Error> {
        futures01::Future::poll(&mut self.0)
    }
}

pub trait Futures01CompatExt: futures01::Future + Sized
where
    Self::Error: Into<Error>,
{
    fn compat01(self) -> Compat01<Self> {
        Compat01::from(self)
    }
}

impl<F> Futures01CompatExt for F
where
    F: futures01::Future,
    F::Error: Into<Error>,
{
}

#[derive(Debug)]
#[must_use = "futures do nothing unless polled."]
pub enum MaybeDone<F: TryFuture> {
    Ready(F::Ok),
    Pending(F),
    Gone,
}

impl<F: TryFuture> MaybeDone<F> {
    pub fn take_item(&mut self) -> Option<F::Ok> {
        match std::mem::replace(self, MaybeDone::Gone) {
            MaybeDone::Ready(output) => Some(output),
            _ => None,
        }
    }
}

impl<F: TryFuture> TryFuture for MaybeDone<F> {
    type Ok = ();
    type Error = F::Error;

    fn poll_ready(&mut self, input: &mut Input<'_>) -> Poll<Self::Ok, Self::Error> {
        let polled = match self {
            MaybeDone::Ready(..) => return Ok(Async::Ready(())),
            MaybeDone::Pending(ref mut future) => future.poll_ready(input)?,
            MaybeDone::Gone => panic!("This future has already polled"),
        };
        match polled {
            Async::Ready(output) => {
                *self = MaybeDone::Ready(output);
                Ok(Async::Ready(()))
            }
            Async::NotReady => Ok(Async::NotReady),
        }
    }
}