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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
cfg_unstable! {
    mod delay;
    mod flatten;
    mod race;
    mod try_race;
    mod join;
    mod try_join;

    use std::time::Duration;
    use delay::DelayFuture;
    use flatten::FlattenFuture;
    use crate::future::IntoFuture;
    use race::Race;
    use try_race::TryRace;
    use join::Join;
    use try_join::TryJoin;
}

cfg_unstable_default! {
    use crate::future::timeout::TimeoutFuture;
}

pub use core::future::Future as Future;

#[doc = r#"
    Extension methods for [`Future`].

    [`Future`]: ../future/trait.Future.html
"#]
#[cfg(any(feature = "std", feature = "docs"))]
pub trait FutureExt: Future {
    /// Returns a Future that delays execution for a specified time.
    ///
    /// # Examples
    ///
    /// ```
    /// # async_std::task::block_on(async {
    /// use async_std::prelude::*;
    /// use async_std::future;
    /// use std::time::Duration;
    ///
    /// let a = future::ready(1).delay(Duration::from_millis(2000));
    /// dbg!(a.await);
    /// # })
    /// ```
    #[cfg(feature = "unstable")]
    #[cfg_attr(feature = "docs", doc(cfg(unstable)))]
    fn delay(self, dur: Duration) -> DelayFuture<Self>
    where
        Self: Sized,
    {
        DelayFuture::new(self, dur)
    }

    /// Flatten out the execution of this future when the result itself
    /// can be converted into another future.
    ///
    /// # Examples
    ///
    /// ```
    /// # async_std::task::block_on(async {
    /// use async_std::prelude::*;
    ///
    /// let nested_future = async { async { 1 } };
    /// let future = nested_future.flatten();
    /// assert_eq!(future.await, 1);
    /// # })
    /// ```
    #[cfg(feature = "unstable")]
    #[cfg_attr(feature = "docs", doc(cfg(unstable)))]
    fn flatten(
        self,
    ) -> FlattenFuture<Self, <Self::Output as IntoFuture>::Future>
    where
        Self: Sized,
        <Self as Future>::Output: IntoFuture,
    {
       FlattenFuture::new(self)
    }

    #[doc = r#"
        Waits for one of two similarly-typed futures to complete.

        Awaits multiple futures simultaneously, returning the output of the
        first future that completes.

        This function will return a new future which awaits for either one of both
        futures to complete. If multiple futures are completed at the same time,
        resolution will occur in the order that they have been passed.

        Note that this function consumes all futures passed, and once a future is
        completed, all other futures are dropped.

        # Examples

        ```
        # async_std::task::block_on(async {
        use async_std::prelude::*;
        use async_std::future;

        let a = future::pending();
        let b = future::ready(1u8);
        let c = future::ready(2u8);

        let f = a.race(b).race(c);
        assert_eq!(f.await, 1u8);
        # });
        ```
    "#]
    #[cfg(feature = "unstable")]
    #[cfg_attr(feature = "docs", doc(cfg(unstable)))]
    fn race<F>(
        self,
        other: F,
    ) -> Race<Self, F>
    where
        Self: std::future::Future + Sized,
        F: std::future::Future<Output = <Self as std::future::Future>::Output>,
    {
        Race::new(self, other)
    }

    #[doc = r#"
        Waits for one of two similarly-typed fallible futures to complete.

        Awaits multiple futures simultaneously, returning all results once complete.

        `try_race` is similar to [`race`], but keeps going if a future
        resolved to an error until all futures have been resolved. In which case
        an error is returned.

        The ordering of which value is yielded when two futures resolve
        simultaneously is intentionally left unspecified.

        [`race`]: #method.race

        # Examples

        ```
        # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
        #
        use async_std::prelude::*;
        use async_std::future;
        use std::io::{Error, ErrorKind};

        let a = future::pending::<Result<_, Error>>();
        let b = future::ready(Err(Error::from(ErrorKind::Other)));
        let c = future::ready(Ok(1u8));

        let f = a.try_race(b).try_race(c);
        assert_eq!(f.await?, 1u8);
        #
        # Ok(()) }) }
        ```
    "#]
    #[cfg(feature = "unstable")]
    #[cfg_attr(feature = "docs", doc(cfg(unstable)))]
    fn try_race<F, T, E>(
        self,
        other: F
    ) -> TryRace<Self, F>
    where
        Self: std::future::Future<Output = Result<T, E>> + Sized,
        F: std::future::Future<Output = <Self as std::future::Future>::Output>,
    {
        TryRace::new(self, other)
    }

    #[doc = r#"
        Waits for two similarly-typed futures to complete.

        Awaits multiple futures simultaneously, returning the output of the
        futures once both complete.

        This function returns a new future which polls both futures
        concurrently.

        # Examples

        ```
        # async_std::task::block_on(async {
        use async_std::prelude::*;
        use async_std::future;

        let a = future::ready(1u8);
        let b = future::ready(2u16);

        let f = a.join(b);
        assert_eq!(f.await, (1u8, 2u16));
        # });
        ```
    "#]
    #[cfg(any(feature = "unstable", feature = "docs"))]
    #[cfg_attr(feature = "docs", doc(cfg(unstable)))]
    fn join<F>(
        self,
        other: F
    ) -> Join<Self, F>
    where
        Self: std::future::Future + Sized,
        F: std::future::Future,
    {
        Join::new(self, other)
    }

    #[doc = r#"
        Waits for two similarly-typed fallible futures to complete.

        Awaits multiple futures simultaneously, returning all results once
        complete.

        `try_join` is similar to [`join`], but returns an error immediately
        if a future resolves to an error.

        [`join`]: #method.join

        # Examples

        ```
        # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
        #
        use async_std::prelude::*;
        use async_std::future;

        let a = future::ready(Err::<u8, &str>("Error"));
        let b = future::ready(Ok(1u8));

        let f = a.try_join(b);
        assert_eq!(f.await, Err("Error"));

        let a = future::ready(Ok::<u8, String>(1u8));
        let b = future::ready(Ok::<u16, String>(2u16));

        let f = a.try_join(b);
        assert_eq!(f.await, Ok((1u8, 2u16)));
        #
        # Ok(()) }) }
        ```
    "#]
    #[cfg(any(feature = "unstable", feature = "docs"))]
    #[cfg_attr(feature = "docs", doc(cfg(unstable)))]
    fn try_join<F, A, B, E>(
        self,
        other: F
    ) -> TryJoin<Self, F>
    where
        Self: std::future::Future<Output = Result<A, E>> + Sized,
        F: std::future::Future<Output = Result<B, E>>,
    {
        TryJoin::new(self, other)
    }

    #[doc = r#"
        Waits for both the future and a timeout, if the timeout completes before
        the future, it returns a TimeoutError.

        # Example
        ```
        # async_std::task::block_on(async {
        #
        use std::time::Duration;

        use async_std::prelude::*;
        use async_std::future;

        let fut = future::ready(0);
        let dur = Duration::from_millis(100);
        let res = fut.timeout(dur).await;
        assert!(res.is_ok());

        let fut = future::pending::<()>();
        let dur = Duration::from_millis(100);
        let res = fut.timeout(dur).await;
        assert!(res.is_err())
        #
        # });
        ```
    "#]
    #[cfg(any(all(feature = "default", feature = "unstable"), feature = "docs"))]
    #[cfg_attr(feature = "docs", doc(cfg(unstable)))]
    fn timeout(self, dur: Duration) -> TimeoutFuture<Self>
        where Self: Sized
    {
        TimeoutFuture::new(self, dur)
    }
}

#[cfg(any(feature = "std", feature = "docs"))]
impl<T: Future + ?Sized> FutureExt for T {}