1
1
use std:: cmp:: Ordering ;
2
2
use std:: pin:: Pin ;
3
3
4
+ use pin_project_lite:: pin_project;
5
+
4
6
use crate :: future:: Future ;
5
7
use crate :: stream:: Stream ;
6
8
use crate :: task:: { Context , Poll } ;
7
9
8
- #[ doc( hidden) ]
9
- #[ allow( missing_debug_implementations) ]
10
- pub struct MaxByFuture < S , F , T > {
11
- stream : S ,
12
- compare : F ,
13
- max : Option < T > ,
10
+ pin_project ! {
11
+ #[ doc( hidden) ]
12
+ #[ allow( missing_debug_implementations) ]
13
+ pub struct MaxByFuture <S , F , T > {
14
+ #[ pin]
15
+ stream: S ,
16
+ compare: F ,
17
+ max: Option <T >,
18
+ }
14
19
}
15
20
16
21
impl < S , F , T > MaxByFuture < S , F , T > {
17
- pin_utils:: unsafe_pinned!( stream: S ) ;
18
- pin_utils:: unsafe_unpinned!( compare: F ) ;
19
- pin_utils:: unsafe_unpinned!( max: Option <T >) ;
20
-
21
22
pub ( super ) fn new ( stream : S , compare : F ) -> Self {
22
23
MaxByFuture {
23
24
stream,
@@ -35,22 +36,23 @@ where
35
36
{
36
37
type Output = Option < S :: Item > ;
37
38
38
- fn poll ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
39
- let next = futures_core:: ready!( self . as_mut( ) . stream( ) . poll_next( cx) ) ;
39
+ fn poll ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
40
+ let this = self . project ( ) ;
41
+ let next = futures_core:: ready!( this. stream. poll_next( cx) ) ;
40
42
41
43
match next {
42
44
Some ( new) => {
43
45
cx. waker ( ) . wake_by_ref ( ) ;
44
- match self . as_mut ( ) . max ( ) . take ( ) {
45
- None => * self . as_mut ( ) . max ( ) = Some ( new) ,
46
- Some ( old) => match ( & mut self . as_mut ( ) . compare ( ) ) ( & new, & old) {
47
- Ordering :: Greater => * self . as_mut ( ) . max ( ) = Some ( new) ,
48
- _ => * self . as_mut ( ) . max ( ) = Some ( old) ,
46
+ match this . max . take ( ) {
47
+ None => * this . max = Some ( new) ,
48
+ Some ( old) => match ( this . compare ) ( & new, & old) {
49
+ Ordering :: Greater => * this . max = Some ( new) ,
50
+ _ => * this . max = Some ( old) ,
49
51
} ,
50
52
}
51
53
Poll :: Pending
52
54
}
53
- None => Poll :: Ready ( self . max ) ,
55
+ None => Poll :: Ready ( * this . max ) ,
54
56
}
55
57
}
56
58
}
0 commit comments