@@ -7,60 +7,50 @@ use futures_timer::Delay;
7
7
use crate :: stream:: Stream ;
8
8
use crate :: task:: { Context , Poll } ;
9
9
10
- /// A stream that only yields one element once every `duration`, and drops all others .
10
+ /// A stream that only yields one element once every `duration`, and applies backpressure. Does not drop any elements .
11
11
/// #[doc(hidden)]
12
12
#[ allow( missing_debug_implementations) ]
13
- pub struct Throttle < S , T > {
13
+ pub struct Throttle < S > {
14
14
stream : S ,
15
15
duration : Duration ,
16
16
delay : Option < Delay > ,
17
- last : Option < T > ,
18
17
}
19
18
20
- impl < S : Unpin , T > Unpin for Throttle < S , T > { }
19
+ impl < S : Unpin > Unpin for Throttle < S > { }
21
20
22
- impl < S : Stream > Throttle < S , S :: Item > {
21
+ impl < S : Stream > Throttle < S > {
23
22
pin_utils:: unsafe_pinned!( stream: S ) ;
24
23
pin_utils:: unsafe_unpinned!( duration: Duration ) ;
25
24
pin_utils:: unsafe_pinned!( delay: Option <Delay >) ;
26
- pin_utils:: unsafe_unpinned!( last: Option <S :: Item >) ;
27
25
28
26
pub ( super ) fn new ( stream : S , duration : Duration ) -> Self {
29
27
Throttle {
30
28
stream,
31
29
duration,
32
30
delay : None ,
33
- last : None ,
34
31
}
35
32
}
36
33
}
37
34
38
- impl < S : Stream > Stream for Throttle < S , S :: Item > {
35
+ impl < S : Stream > Stream for Throttle < S > {
39
36
type Item = S :: Item ;
40
37
41
38
fn poll_next ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Option < S :: Item > > {
42
39
if let Some ( d) = self . as_mut ( ) . delay ( ) . as_pin_mut ( ) {
43
40
if d. poll ( cx) . is_ready ( ) {
44
- if let Some ( v) = self . as_mut ( ) . last ( ) . take ( ) {
45
- // Sets last to None.
46
- * self . as_mut ( ) . delay ( ) = Some ( Delay :: new ( self . duration ) ) ;
47
- return Poll :: Ready ( Some ( v) ) ;
48
- } else {
49
- * self . as_mut ( ) . delay ( ) = None ;
50
- }
41
+ * self . as_mut ( ) . delay ( ) = None ;
42
+ } else {
43
+ return Poll :: Pending ;
51
44
}
52
45
}
53
46
54
47
match self . as_mut ( ) . stream ( ) . poll_next ( cx) {
55
- Poll :: Pending => Poll :: Pending ,
56
- Poll :: Ready ( None ) => return Poll :: Ready ( None ) ,
48
+ Poll :: Pending => {
49
+ cx. waker ( ) . wake_by_ref ( ) ; // Continue driving even though emitting Pending
50
+ Poll :: Pending
51
+ }
52
+ Poll :: Ready ( None ) => Poll :: Ready ( None ) ,
57
53
Poll :: Ready ( Some ( v) ) => {
58
- if self . as_mut ( ) . delay ( ) . is_some ( ) {
59
- * self . as_mut ( ) . last ( ) = Some ( v) ;
60
- cx. waker ( ) . wake_by_ref ( ) ; // Continue driving even though emitting Pending
61
- return Poll :: Pending ;
62
- }
63
-
64
54
* self . as_mut ( ) . delay ( ) = Some ( Delay :: new ( self . duration ) ) ;
65
55
Poll :: Ready ( Some ( v) )
66
56
}
0 commit comments