1
- use super :: assert_future;
1
+ use super :: { assert_future, Biased , Fair , IsBiased } ;
2
2
use crate :: future:: { Either , FutureExt } ;
3
+ use core:: marker:: PhantomData ;
3
4
use core:: pin:: Pin ;
4
5
use futures_core:: future:: { FusedFuture , Future } ;
5
6
use futures_core:: task:: { Context , Poll } ;
6
7
7
8
/// Future for the [`select()`] function.
8
9
#[ must_use = "futures do nothing unless you `.await` or poll them" ]
9
10
#[ derive( Debug ) ]
10
- pub struct Select < A , B > {
11
+ pub struct Select < A , B , BIASED = Fair > {
11
12
inner : Option < ( A , B ) > ,
12
- _biased : bool ,
13
+ _phantom : PhantomData < BIASED > ,
13
14
}
14
15
15
- impl < A : Unpin , B : Unpin > Unpin for Select < A , B > { }
16
+ impl < A : Unpin , B : Unpin , BIASED > Unpin for Select < A , B , BIASED > { }
16
17
17
18
/// Waits for either one of two differently-typed futures to complete.
18
19
///
@@ -86,14 +87,14 @@ impl<A: Unpin, B: Unpin> Unpin for Select<A, B> {}
86
87
/// })
87
88
/// }
88
89
/// ```
89
- pub fn select < A , B > ( future1 : A , future2 : B ) -> Select < A , B >
90
+ pub fn select < A , B > ( future1 : A , future2 : B ) -> Select < A , B , Fair >
90
91
where
91
92
A : Future + Unpin ,
92
93
B : Future + Unpin ,
93
94
{
94
95
assert_future :: < Either < ( A :: Output , B ) , ( B :: Output , A ) > , _ > ( Select {
95
96
inner : Some ( ( future1, future2) ) ,
96
- _biased : false ,
97
+ _phantom : PhantomData ,
97
98
} )
98
99
}
99
100
@@ -167,21 +168,22 @@ where
167
168
/// })
168
169
/// }
169
170
/// ```
170
- pub fn select_biased < A , B > ( future1 : A , future2 : B ) -> Select < A , B >
171
+ pub fn select_biased < A , B > ( future1 : A , future2 : B ) -> Select < A , B , Biased >
171
172
where
172
173
A : Future + Unpin ,
173
174
B : Future + Unpin ,
174
175
{
175
176
assert_future :: < Either < ( A :: Output , B ) , ( B :: Output , A ) > , _ > ( Select {
176
177
inner : Some ( ( future1, future2) ) ,
177
- _biased : true ,
178
+ _phantom : PhantomData ,
178
179
} )
179
180
}
180
181
181
- impl < A , B > Future for Select < A , B >
182
+ impl < A , B , BIASED > Future for Select < A , B , BIASED >
182
183
where
183
184
A : Future + Unpin ,
184
185
B : Future + Unpin ,
186
+ BIASED : IsBiased ,
185
187
{
186
188
type Output = Either < ( A :: Output , B ) , ( B :: Output , A ) > ;
187
189
@@ -195,7 +197,6 @@ where
195
197
Some ( value) => value,
196
198
}
197
199
}
198
- let _biased = self . _biased ;
199
200
200
201
let ( a, b) = self . inner . as_mut ( ) . expect ( "cannot poll Select twice" ) ;
201
202
@@ -208,7 +209,7 @@ where
208
209
}
209
210
210
211
#[ cfg( feature = "std" ) ]
211
- if _biased || crate :: gen_index ( 2 ) == 0 {
212
+ if BIASED :: IS_BIASED || crate :: gen_index ( 2 ) == 0 {
212
213
poll_wrap ! ( a, unwrap_option( self . inner. take( ) ) . 1 , Either :: Left ) ;
213
214
poll_wrap ! ( b, unwrap_option( self . inner. take( ) ) . 0 , Either :: Right ) ;
214
215
} else {
@@ -225,10 +226,11 @@ where
225
226
}
226
227
}
227
228
228
- impl < A , B > FusedFuture for Select < A , B >
229
+ impl < A , B , BIASED > FusedFuture for Select < A , B , BIASED >
229
230
where
230
231
A : Future + Unpin ,
231
232
B : Future + Unpin ,
233
+ BIASED : IsBiased ,
232
234
{
233
235
fn is_terminated ( & self ) -> bool {
234
236
self . inner . is_none ( )
0 commit comments