@@ -9,13 +9,14 @@ use crate::fns::FnOnce1;
99pin_project ! {
1010 /// Internal Map future
1111 #[ project = MapProj ]
12+ #[ project_replace = MapProjReplace ]
1213 #[ derive( Debug ) ]
1314 #[ must_use = "futures do nothing unless you `.await` or poll them" ]
1415 pub enum Map <Fut , F > {
1516 Incomplete {
1617 #[ pin]
1718 future: Fut ,
18- f: Option < F > ,
19+ f: F ,
1920 } ,
2021 Complete ,
2122 }
@@ -24,13 +25,14 @@ pin_project! {
2425impl < Fut , F > Map < Fut , F > {
2526 /// Creates a new Map.
2627 pub ( crate ) fn new ( future : Fut , f : F ) -> Self {
27- Self :: Incomplete { future, f : Some ( f ) }
28+ Self :: Incomplete { future, f }
2829 }
2930}
3031
3132impl < Fut , F , T > FusedFuture for Map < Fut , F >
32- where Fut : Future ,
33- F : FnOnce1 < Fut :: Output , Output =T > ,
33+ where
34+ Fut : Future ,
35+ F : FnOnce1 < Fut :: Output , Output = T > ,
3436{
3537 fn is_terminated ( & self ) -> bool {
3638 match self {
@@ -41,20 +43,24 @@ impl<Fut, F, T> FusedFuture for Map<Fut, F>
4143}
4244
4345impl < Fut , F , T > Future for Map < Fut , F >
44- where Fut : Future ,
45- F : FnOnce1 < Fut :: Output , Output =T > ,
46+ where
47+ Fut : Future ,
48+ F : FnOnce1 < Fut :: Output , Output = T > ,
4649{
4750 type Output = T ;
4851
4952 fn poll ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < T > {
5053 match self . as_mut ( ) . project ( ) {
51- MapProj :: Incomplete { future, f } => {
54+ MapProj :: Incomplete { future, .. } => {
5255 let output = ready ! ( future. poll( cx) ) ;
53- let f = f. take ( ) . unwrap ( ) ;
54- self . set ( Self :: Complete ) ;
55- Poll :: Ready ( f. call_once ( output) )
56- } ,
57- MapProj :: Complete => panic ! ( "Map must not be polled after it returned `Poll::Ready`" ) ,
56+ match self . project_replace ( Map :: Complete ) {
57+ MapProjReplace :: Incomplete { f, .. } => Poll :: Ready ( f. call_once ( output) ) ,
58+ MapProjReplace :: Complete => unreachable ! ( ) ,
59+ }
60+ }
61+ MapProj :: Complete => {
62+ panic ! ( "Map must not be polled after it returned `Poll::Ready`" )
63+ }
5864 }
5965 }
6066}
0 commit comments