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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/**************************************************************************************************
 *                                                                                                *
 * This Source Code Form is subject to the terms of the Mozilla Public                            *
 * License, v. 2.0. If a copy of the MPL was not distributed with this                            *
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.                                       *
 *                                                                                                *
 **************************************************************************************************/

// ======================================== Documentation ======================================= \\

//! A way to await on the output of either of two futures.
//!
//! ## Example
//!
//! ```rust
//! use futures_lite::future;
//! use futures_either::{either, Either};
//!
//! # future::block_on(async {
//! #
//! let out = either(
//!     async { 42 },
//!     async { false },
//! ).await;
//! assert_eq!(out, Either::Left(42));
//!
//! let out = either(
//!     future::pending::<bool>(),
//!     async { 42 },
//! ).await;
//! assert_eq!(out, Either::Right(42));
//! #
//! # });
//! ```

// =========================================== Imports ========================================== \\

pub use either::Either;

use core::future::Future;
use core::pin::Pin;
use core::task::{Context, Poll};

// ============================================ Types =========================================== \\

/// The [`Future`s] returned by this crate's functions.
///
/// [`Future`s]: core::future::Future
pub mod futs {
    /// The [`Future`] returned by [`either()`].
    ///
    /// [`Future`]: core::future::Future
    /// [`either()`]: crate::either()
    pub struct Either<L, R> {
        pub(super) left: L,
        pub(super) right: R,
    }

    #[cfg(feature = "fair")]
    #[cfg_attr(docsrs, doc(cfg(feature = "fair")))]
    /// The [`Future`] returned by [`either_fair()`].
    ///
    /// [`Future`]: core::future::Future
    /// [`either_fair()`]: crate::either_fair()
    pub struct EitherFair<L, R> {
        pub(super) left: L,
        pub(super) right: R,
    }

    /// The [`Future`] returned by [`try_either()`].
    ///
    /// [`Future`]: core::future::Future
    /// [`try_either()`]: crate::try_either()
    pub struct TryEither<L, R> {
        pub(super) fut: Either<L, R>,
    }

    #[cfg(feature = "fair")]
    #[cfg_attr(docsrs, doc(cfg(feature = "fair")))]
    /// The [`Future`] returned by [`try_either_fair()`].
    ///
    /// [`Future`]: core::future::Future
    /// [`try_either_fair()`]: crate::try_either_fair()
    pub struct TryEitherFair<L, R> {
        pub(super) fut: EitherFair<L, R>,
    }
}

// ========================================== either() ========================================== \\

/// Returns a future polling two futures and returning the output of the first one to complete.
///
/// The returned future will always poll `left` first; for a "fair" alternative, see
/// [`either_fair()`].
///
/// ## Example
///
/// ```rust
/// use futures_lite::future;
/// use futures_either::{either, Either};
///
/// # future::block_on(async {
/// #
/// let out = either(
///     async { 42 },
///     async { false },
/// ).await;
/// assert_eq!(out, Either::Left(42));
///
/// let out = either(
///     future::pending::<bool>(),
///     async { 42 },
/// ).await;
/// assert_eq!(out, Either::Right(42));
/// #
/// # });
/// ```
pub fn either<L, R>(left: L, right: R) -> futs::Either<L, R>
where
    L: Future,
    R: Future,
{
    futs::Either { left, right }
}

// ======================================== either_fair() ======================================= \\

#[cfg(feature = "fair")]
#[cfg_attr(docsrs, doc(cfg(feature = "fair")))]
/// Returns a future polling two futures and returning the output of the first one to complete.
///
/// The returned future will choose which future to poll first randomly, each time it is being
/// polled; for an "unfair" alternative, see [`either()`].
///
/// ## Example
///
/// ```rust
/// use futures_lite::future;
/// use futures_either::{either_fair, Either};
///
/// # future::block_on(async {
/// #
/// let out = either_fair(
///     async { 42 },
///     async { false },
/// ).await;
/// assert!(out == Either::Left(42) || out == Either::Right(false));
///
/// let out = either_fair(
///     future::pending::<bool>(),
///     async { 42 },
/// ).await;
/// assert_eq!(out, Either::Right(42));
/// #
/// # });
/// ```
pub fn either_fair<L, R>(left: L, right: R) -> futs::EitherFair<L, R>
where
    L: Future,
    R: Future,
{
    futs::EitherFair { left, right }
}

// ======================================== try_either() ======================================== \\

/// Returns a future polling two futures and returning a result with the output or error returned
/// by the first one to complete.
///
/// The returned future will always poll `left` first; for a "fair" alternative, see
/// [`try_either_fair()`].
///
/// ## Example
///
/// ```rust
/// use futures_lite::future;
/// use futures_either::{try_either, Either};
///
/// # future::block_on(async {
/// #
/// let out = try_either(
///     async { Ok(42) },
///     async { Result::<bool, bool>::Err(false) },
/// ).await;
/// assert_eq!(out, Ok(Either::Left(42)));
///
/// let out = try_either(
///     future::pending::<Result<bool, i32>>(),
///     async { Result::<i32, i32>::Err(42) },
/// ).await;
/// assert_eq!(out, Err(42));
/// #
/// # });
/// ```
pub fn try_either<OL, OR, E, L, R>(left: L, right: R) -> futs::TryEither<L, R>
where
    L: Future<Output = Result<OL, E>>,
    R: Future<Output = Result<OR, E>>,
{
    futs::TryEither { fut: either(left, right), }
}

// ====================================== try_either_fair() ===================================== \\

#[cfg(feature = "fair")]
#[cfg_attr(docsrs, doc(cfg(feature = "fair")))]
/// Returns a future polling two futures and returning a result with the ouput or error returned by
/// the first one to complete.
///
/// The returned future will choose which future to poll first randomly, each time it is being
/// polled; for an "unfair" alternative, see [`try_either()`].
///
/// ## Example
///
/// ```rust
/// use futures_lite::future;
/// use futures_either::{try_either_fair, Either};
///
/// # future::block_on(async {
/// #
/// let out = try_either_fair(
///     async { Ok(42) },
///     async { Result::<bool, bool>::Err(false) },
/// ).await;
/// assert!(out == Ok(Either::Left(42)) || out == Err(false));
///
/// let out = try_either_fair(
///     future::pending::<Result<bool, i32>>(),
///     async { Result::<i32, i32>::Err(42) },
/// ).await;
/// assert_eq!(out, Err(42));
/// #
/// # });
/// ```
pub fn try_either_fair<OL, OR, E, L, R>(left: L, right: R) -> futs::TryEitherFair<L, R>
where
    L: Future<Output = Result<OL, E>>,
    R: Future<Output = Result<OR, E>>,
{
    futs::TryEitherFair { fut: either_fair(left, right), }
}

// ========================================= impl Future ======================================== \\

impl<L, R> Future for futs::Either<L, R>
where
    L: Future,
    R: Future,
{
    type Output = Either<L::Output, R::Output>;

    fn poll(self: Pin<&mut Self>, ctx: &mut Context) -> Poll<Self::Output> {
        let this = unsafe { self.get_unchecked_mut() };

        if let Poll::Ready(out) = unsafe { Pin::new_unchecked(&mut this.left) }.poll(ctx) {
            return Poll::Ready(Either::Left(out));
        }

        if let Poll::Ready(out) = unsafe { Pin::new_unchecked(&mut this.right) }.poll(ctx) {
            return Poll::Ready(Either::Right(out));
        }

        Poll::Pending
    }
}

#[cfg(feature = "fair")]
impl<L, R> Future for futs::EitherFair<L, R>
where
    L: Future,
    R: Future,
{
    type Output = Either<L::Output, R::Output>;

    fn poll(self: Pin<&mut Self>, ctx: &mut Context) -> Poll<Self::Output> {
        let this = unsafe { self.get_unchecked_mut() };

        if fastrand::bool() {
            if let Poll::Ready(out) = unsafe { Pin::new_unchecked(&mut this.left) }.poll(ctx) {
                return Poll::Ready(Either::Left(out));
            }

            if let Poll::Ready(out) = unsafe { Pin::new_unchecked(&mut this.right) }.poll(ctx) {
                return Poll::Ready(Either::Right(out));
            }
        } else {
            if let Poll::Ready(out) = unsafe { Pin::new_unchecked(&mut this.right) }.poll(ctx) {
                return Poll::Ready(Either::Right(out));
            }
           
            if let Poll::Ready(out) = unsafe { Pin::new_unchecked(&mut this.left) }.poll(ctx) {
                return Poll::Ready(Either::Left(out));
            }
        }

        Poll::Pending
    }
}

impl<OL, OR, E, L, R> Future for futs::TryEither<L, R>
where
    L: Future<Output = Result<OL, E>>,
    R: Future<Output = Result<OR, E>>,
{
    type Output = Result<Either<OL, OR>, E>;

    fn poll(self: Pin<&mut Self>, ctx: &mut Context) -> Poll<Self::Output> {
        let this = unsafe { self.get_unchecked_mut() };

        if let Poll::Ready(out) = unsafe { Pin::new_unchecked(&mut this.fut) }.poll(ctx) {
            match out {
                Either::Left(Ok(left)) => Ok(Either::Left(left)),
                Either::Right(Ok(right)) => Ok(Either::Right(right)),
                Either::Left(Err(err)) | Either::Right(Err(err)) => Err(err),
            }.into()
        } else {
            Poll::Pending
        }
    }
}

#[cfg(feature = "fair")]
impl<OL, OR, E, L, R> Future for futs::TryEitherFair<L, R>
where
    L: Future<Output = Result<OL, E>>,
    R: Future<Output = Result<OR, E>>,
{
    type Output = Result<Either<OL, OR>, E>;

    fn poll(self: Pin<&mut Self>, ctx: &mut Context) -> Poll<Self::Output> {
        let this = unsafe { self.get_unchecked_mut() };

        if let Poll::Ready(out) = unsafe { Pin::new_unchecked(&mut this.fut) }.poll(ctx) {
            match out {
                Either::Left(Ok(left)) => Ok(Either::Left(left)),
                Either::Right(Ok(right)) => Ok(Either::Right(right)),
                Either::Left(Err(err)) | Either::Right(Err(err)) => Err(err),
            }.into()
        } else {
            Poll::Pending
        }
    }
}