File tree Expand file tree Collapse file tree 2 files changed +12
-7
lines changed Expand file tree Collapse file tree 2 files changed +12
-7
lines changed Original file line number Diff line number Diff line change @@ -318,7 +318,7 @@ extension_trait! {
318318 #[ doc = r#"
319319 Limit the amount of items yielded per timeslice in a stream.
320320
321- This stream does not drop any items, but will only limit the rate at which items pass through.
321+ This stream does not drop any items, but will only limit the rate at which items pass through.
322322 # Examples
323323 ```
324324 # fn main() { async_std::task::block_on(async {
Original file line number Diff line number Diff line change 11use std:: future:: Future ;
22use std:: pin:: Pin ;
3- use std:: time:: Duration ;
3+ use std:: time:: { Duration , Instant } ;
44
55use futures_timer:: Delay ;
66use pin_project_lite:: pin_project;
@@ -23,7 +23,9 @@ pin_project! {
2323 stream: S ,
2424 duration: Duration ,
2525 #[ pin]
26- delay: Option <Delay >,
26+ blocked: bool ,
27+ #[ pin]
28+ delay: Delay ,
2729 }
2830}
2931
@@ -32,7 +34,8 @@ impl<S: Stream> Throttle<S> {
3234 Throttle {
3335 stream,
3436 duration,
35- delay : None ,
37+ blocked : false ,
38+ delay : Delay :: new ( Duration :: default ( ) ) ,
3639 }
3740 }
3841}
@@ -42,9 +45,10 @@ impl<S: Stream> Stream for Throttle<S> {
4245
4346 fn poll_next ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Option < S :: Item > > {
4447 let mut this = self . project ( ) ;
45- if let Some ( d) = this. delay . as_mut ( ) . as_pin_mut ( ) {
48+ if * this. blocked {
49+ let d = this. delay . as_mut ( ) ;
4650 if d. poll ( cx) . is_ready ( ) {
47- this. delay . set ( None ) ;
51+ * this. blocked = false ;
4852 } else {
4953 return Poll :: Pending ;
5054 }
@@ -57,7 +61,8 @@ impl<S: Stream> Stream for Throttle<S> {
5761 }
5862 Poll :: Ready ( None ) => Poll :: Ready ( None ) ,
5963 Poll :: Ready ( Some ( v) ) => {
60- this. delay . set ( Some ( Delay :: new ( * this. duration ) ) ) ;
64+ * this. blocked = true ;
65+ this. delay . reset ( Instant :: now ( ) + * this. duration ) ;
6166 Poll :: Ready ( Some ( v) )
6267 }
6368 }
You can’t perform that action at this time.
0 commit comments