@@ -7,60 +7,50 @@ use futures_timer::Delay;
77use crate :: stream:: Stream ;
88use crate :: task:: { Context , Poll } ;
99
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 .
1111/// #[doc(hidden)]
1212#[ allow( missing_debug_implementations) ]
13- pub struct Throttle < S , T > {
13+ pub struct Throttle < S > {
1414 stream : S ,
1515 duration : Duration ,
1616 delay : Option < Delay > ,
17- last : Option < T > ,
1817}
1918
20- impl < S : Unpin , T > Unpin for Throttle < S , T > { }
19+ impl < S : Unpin > Unpin for Throttle < S > { }
2120
22- impl < S : Stream > Throttle < S , S :: Item > {
21+ impl < S : Stream > Throttle < S > {
2322 pin_utils:: unsafe_pinned!( stream: S ) ;
2423 pin_utils:: unsafe_unpinned!( duration: Duration ) ;
2524 pin_utils:: unsafe_pinned!( delay: Option <Delay >) ;
26- pin_utils:: unsafe_unpinned!( last: Option <S :: Item >) ;
2725
2826 pub ( super ) fn new ( stream : S , duration : Duration ) -> Self {
2927 Throttle {
3028 stream,
3129 duration,
3230 delay : None ,
33- last : None ,
3431 }
3532 }
3633}
3734
38- impl < S : Stream > Stream for Throttle < S , S :: Item > {
35+ impl < S : Stream > Stream for Throttle < S > {
3936 type Item = S :: Item ;
4037
4138 fn poll_next ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Option < S :: Item > > {
4239 if let Some ( d) = self . as_mut ( ) . delay ( ) . as_pin_mut ( ) {
4340 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 ;
5144 }
5245 }
5346
5447 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 ) ,
5753 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-
6454 * self . as_mut ( ) . delay ( ) = Some ( Delay :: new ( self . duration ) ) ;
6555 Poll :: Ready ( Some ( v) )
6656 }
0 commit comments