@@ -2,20 +2,20 @@ use std::cmp::Ordering;
2
2
use std:: pin:: Pin ;
3
3
4
4
use crate :: future:: Future ;
5
- use crate :: stream:: Stream ;
6
5
use crate :: task:: { Context , Poll } ;
7
6
8
- /// A future that yields the minimum item in a stream by a given comparison function.
9
- #[ derive( Clone , Debug ) ]
10
- pub struct MinByFuture < S : Stream , F > {
7
+ #[ allow( missing_debug_implementations) ]
8
+ pub struct MinByFuture < S , F , T > {
11
9
stream : S ,
12
10
compare : F ,
13
- min : Option < S :: Item > ,
11
+ min : Option < T > ,
14
12
}
15
13
16
- impl < S : Stream + Unpin , F > Unpin for MinByFuture < S , F > { }
14
+ impl < S , F , T > MinByFuture < S , F , T > {
15
+ pin_utils:: unsafe_pinned!( stream: S ) ;
16
+ pin_utils:: unsafe_unpinned!( compare: F ) ;
17
+ pin_utils:: unsafe_unpinned!( min: Option <T >) ;
17
18
18
- impl < S : Stream + Unpin , F > MinByFuture < S , F > {
19
19
pub ( super ) fn new ( stream : S , compare : F ) -> Self {
20
20
MinByFuture {
21
21
stream,
@@ -25,25 +25,25 @@ impl<S: Stream + Unpin, F> MinByFuture<S, F> {
25
25
}
26
26
}
27
27
28
- impl < S , F > Future for MinByFuture < S , F >
28
+ impl < S , F > Future for MinByFuture < S , F , S :: Item >
29
29
where
30
- S : futures_core:: stream:: Stream + Unpin ,
30
+ S : futures_core:: stream:: Stream + Unpin + Sized ,
31
31
S :: Item : Copy ,
32
32
F : FnMut ( & S :: Item , & S :: Item ) -> Ordering ,
33
33
{
34
34
type Output = Option < S :: Item > ;
35
35
36
36
fn poll ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
37
- let next = futures_core:: ready!( Pin :: new ( & mut self . stream) . poll_next( cx) ) ;
37
+ let next = futures_core:: ready!( self . as_mut ( ) . stream( ) . poll_next( cx) ) ;
38
38
39
39
match next {
40
40
Some ( new) => {
41
41
cx. waker ( ) . wake_by_ref ( ) ;
42
- match self . as_mut ( ) . min . take ( ) {
43
- None => self . as_mut ( ) . min = Some ( new) ,
44
- Some ( old) => match ( & mut self . as_mut ( ) . compare ) ( & new, & old) {
45
- Ordering :: Less => self . as_mut ( ) . min = Some ( new) ,
46
- _ => self . as_mut ( ) . min = Some ( old) ,
42
+ match self . as_mut ( ) . min ( ) . take ( ) {
43
+ None => * self . as_mut ( ) . min ( ) = Some ( new) ,
44
+ Some ( old) => match ( & mut self . as_mut ( ) . compare ( ) ) ( & new, & old) {
45
+ Ordering :: Less => * self . as_mut ( ) . min ( ) = Some ( new) ,
46
+ _ => * self . as_mut ( ) . min ( ) = Some ( old) ,
47
47
} ,
48
48
}
49
49
Poll :: Pending
0 commit comments