1
+ use std:: future:: Future ;
1
2
use std:: pin:: Pin ;
2
3
3
- use crate :: future:: Future ;
4
+ use pin_project_lite:: pin_project;
5
+
4
6
use crate :: stream:: Stream ;
5
7
use crate :: task:: { Context , Poll } ;
6
8
7
- #[ doc( hidden) ]
8
- #[ allow( missing_debug_implementations) ]
9
- pub struct CountFuture < S > {
10
- stream : S ,
11
- count : usize ,
9
+ pin_project ! {
10
+ #[ doc( hidden) ]
11
+ #[ allow( missing_debug_implementations) ]
12
+ pub struct CountFuture <S > {
13
+ #[ pin]
14
+ stream: S ,
15
+ count: usize ,
16
+ }
12
17
}
13
18
14
19
impl < S > CountFuture < S > {
15
- pin_utils:: unsafe_pinned!( stream: S ) ;
16
- pin_utils:: unsafe_unpinned!( count: usize ) ;
17
-
18
20
pub ( crate ) fn new ( stream : S ) -> Self {
19
21
CountFuture { stream, count : 0 }
20
22
}
@@ -26,16 +28,17 @@ where
26
28
{
27
29
type Output = usize ;
28
30
29
- fn poll ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
30
- let next = futures_core:: ready!( self . as_mut( ) . stream( ) . poll_next( cx) ) ;
31
+ fn poll ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
32
+ let this = self . project ( ) ;
33
+ let next = futures_core:: ready!( this. stream. poll_next( cx) ) ;
31
34
32
35
match next {
33
36
Some ( _) => {
34
37
cx. waker ( ) . wake_by_ref ( ) ;
35
- * self . as_mut ( ) . count ( ) += 1 ;
38
+ * this . count += 1 ;
36
39
Poll :: Pending
37
40
}
38
- None => Poll :: Ready ( self . count ) ,
41
+ None => Poll :: Ready ( * this . count ) ,
39
42
}
40
43
}
41
44
}
0 commit comments