@@ -5,17 +5,23 @@ use crate::future::Future;
5
5
use crate :: future:: IntoFuture ;
6
6
use crate :: task:: { Context , Poll } ;
7
7
8
- #[ doc( hidden) ]
9
8
#[ derive( Debug ) ]
10
- pub enum FlattenFuture < Fut1 , Fut2 > {
9
+ pub struct FlattenFuture < Fut1 , Fut2 > {
10
+ state : State < Fut1 , Fut2 > ,
11
+ }
12
+
13
+ #[ derive( Debug ) ]
14
+ enum State < Fut1 , Fut2 > {
11
15
First ( Fut1 ) ,
12
16
Second ( Fut2 ) ,
13
17
Empty ,
14
18
}
15
19
16
20
impl < Fut1 , Fut2 > FlattenFuture < Fut1 , Fut2 > {
17
21
pub fn new ( future : Fut1 ) -> FlattenFuture < Fut1 , Fut2 > {
18
- FlattenFuture :: First ( future)
22
+ FlattenFuture {
23
+ state : State :: First ( future) ,
24
+ }
19
25
}
20
26
}
21
27
@@ -27,19 +33,19 @@ where
27
33
type Output = <Fut1 :: Output as IntoFuture >:: Output ;
28
34
29
35
fn poll ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
30
- let this = unsafe { self . get_unchecked_mut ( ) } ;
36
+ let Self { state } = unsafe { self . get_unchecked_mut ( ) } ;
31
37
loop {
32
- match this {
33
- FlattenFuture :: First ( fut1) => {
38
+ match state {
39
+ State :: First ( fut1) => {
34
40
let fut2 = ready ! ( unsafe { Pin :: new_unchecked( fut1) } . poll( cx) ) . into_future ( ) ;
35
- * this = FlattenFuture :: Second ( fut2) ;
41
+ * state = State :: Second ( fut2) ;
36
42
}
37
- FlattenFuture :: Second ( fut2) => {
43
+ State :: Second ( fut2) => {
38
44
let v = ready ! ( unsafe { Pin :: new_unchecked( fut2) } . poll( cx) ) ;
39
- * this = FlattenFuture :: Empty ;
45
+ * state = State :: Empty ;
40
46
return Poll :: Ready ( v) ;
41
47
}
42
- FlattenFuture :: Empty => unreachable ! ( ) ,
48
+ State :: Empty => panic ! ( "polled a completed future" ) ,
43
49
}
44
50
}
45
51
}
0 commit comments