futures_race/lib.rs
1/**************************************************************************************************
2 * *
3 * This Source Code Form is subject to the terms of the Mozilla Public *
4 * License, v. 2.0. If a copy of the MPL was not distributed with this *
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. *
6 * *
7 **************************************************************************************************/
8
9// ======================================== Configuration ======================================= \\
10
11#![no_std]
12
13// ======================================== Documentation ======================================= \\
14
15//! Deprecated in favor of [`futures-micro`](https://github.com/irrustible/futures-micro) and
16//! [`futures-lite`](https://github.com/stjepang/futures-lite).
17
18// =========================================== Imports ========================================== \\
19
20use core::future::Future;
21use core::pin::Pin;
22use core::task::{Context, Poll};
23use pin_project_lite::pin_project;
24
25// ============================================ Types =========================================== \\
26
27pin_project! {
28 #[derive(Debug)]
29 #[deprecated(since = "1.2.0", note = "please use `futures-micro` or `futures-lite` instead")]
30 /// Deprecated in favor of [`futures-micro`](https://github.com/irrustible/futures-micro) and
31 /// [`futures-lite`](https://github.com/stjepang/futures-lite).
32 pub struct Race<Left, Right>
33 where
34 Left: Future,
35 Right: Future<Output = Left::Output>,
36 {
37 #[pin]
38 left: Left,
39 #[pin]
40 right: Right,
41 }
42}
43
44// ========================================= Interfaces ========================================= \\
45
46#[deprecated(since = "1.2.0", note = "please use `futures-micro` or `futures-lite` instead")]
47/// Deprecated in favor of [`futures-micro`](https://github.com/irrustible/futures-micro) and
48/// [`futures-lite`](https://github.com/stjepang/futures-lite).
49pub trait RaceExt: Future {
50 #[deprecated(since = "1.2.0", note = "please use `futures-micro` or `futures-lite` instead")]
51 /// Deprecated in favor of [`futures-micro`](https://github.com/irrustible/futures-micro) and
52 /// [`futures-lite`](https://github.com/stjepang/futures-lite).
53 fn race<With>(self, with: With) -> Race<Self, With>
54 where
55 Self: Sized,
56 With: Future<Output = Self::Output>,
57 {
58 Race {
59 left: self,
60 right: with,
61 }
62 }
63}
64
65impl<Fut: Future> RaceExt for Fut {}
66
67// ========================================= impl Future ======================================== \\
68
69impl<Left, Right> Future for Race<Left, Right>
70where
71 Left: Future,
72 Right: Future<Output = Left::Output>,
73{
74 type Output = Left::Output;
75
76 fn poll(self: Pin<&mut Self>, ctx: &mut Context) -> Poll<Self::Output> {
77 let this = self.project();
78
79 if let poll @ Poll::Ready(_) = this.left.poll(ctx) {
80 return poll;
81 }
82
83 if let poll @ Poll::Ready(_) = this.right.poll(ctx) {
84 return poll;
85 }
86
87 Poll::Pending
88 }
89}