11use std:: cmp:: Ordering ;
22use std:: pin:: Pin ;
33
4+ use pin_project_lite:: pin_project;
5+
46use crate :: future:: Future ;
57use crate :: stream:: Stream ;
68use crate :: task:: { Context , Poll } ;
79
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+ }
1419}
1520
1621impl < 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-
2122 pub ( super ) fn new ( stream : S , compare : F ) -> Self {
2223 MaxByFuture {
2324 stream,
@@ -35,22 +36,23 @@ where
3536{
3637 type Output = Option < S :: Item > ;
3738
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) ) ;
4042
4143 match next {
4244 Some ( new) => {
4345 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) ,
4951 } ,
5052 }
5153 Poll :: Pending
5254 }
53- None => Poll :: Ready ( self . max ) ,
55+ None => Poll :: Ready ( * this . max ) ,
5456 }
5557 }
5658}
0 commit comments