@@ -3,26 +3,25 @@ use std::pin::Pin;
33use std:: time:: Duration ;
44
55use futures_timer:: Delay ;
6+ use pin_project_lite:: pin_project;
67
78use crate :: stream:: Stream ;
89use crate :: task:: { Context , Poll } ;
910
10- /// A stream that only yields one element once every `duration`, and applies backpressure. Does not drop any elements.
11- /// #[doc(hidden)]
12- #[ allow( missing_debug_implementations) ]
13- pub struct Throttle < S > {
14- stream : S ,
15- duration : Duration ,
16- delay : Option < Delay > ,
11+ pin_project ! {
12+ /// A stream that only yields one element once every `duration`, and applies backpressure. Does not drop any elements.
13+ #[ doc( hidden) ]
14+ #[ allow( missing_debug_implementations) ]
15+ pub struct Throttle <S > {
16+ #[ pin]
17+ stream: S ,
18+ duration: Duration ,
19+ #[ pin]
20+ delay: Option <Delay >,
21+ }
1722}
1823
19- impl < S : Unpin > Unpin for Throttle < S > { }
20-
2124impl < S : Stream > Throttle < S > {
22- pin_utils:: unsafe_pinned!( stream: S ) ;
23- pin_utils:: unsafe_unpinned!( duration: Duration ) ;
24- pin_utils:: unsafe_pinned!( delay: Option <Delay >) ;
25-
2625 pub ( super ) fn new ( stream : S , duration : Duration ) -> Self {
2726 Throttle {
2827 stream,
@@ -35,23 +34,24 @@ impl<S: Stream> Throttle<S> {
3534impl < S : Stream > Stream for Throttle < S > {
3635 type Item = S :: Item ;
3736
38- fn poll_next ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Option < S :: Item > > {
39- if let Some ( d) = self . as_mut ( ) . delay ( ) . as_pin_mut ( ) {
37+ fn poll_next ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Option < S :: Item > > {
38+ let mut this = self . project ( ) ;
39+ if let Some ( d) = this. delay . as_mut ( ) . as_pin_mut ( ) {
4040 if d. poll ( cx) . is_ready ( ) {
41- * self . as_mut ( ) . delay ( ) = None ;
41+ this . delay . set ( None ) ;
4242 } else {
4343 return Poll :: Pending ;
4444 }
4545 }
4646
47- match self . as_mut ( ) . stream ( ) . poll_next ( cx) {
47+ match this . stream . poll_next ( cx) {
4848 Poll :: Pending => {
4949 cx. waker ( ) . wake_by_ref ( ) ; // Continue driving even though emitting Pending
5050 Poll :: Pending
5151 }
5252 Poll :: Ready ( None ) => Poll :: Ready ( None ) ,
5353 Poll :: Ready ( Some ( v) ) => {
54- * self . as_mut ( ) . delay ( ) = Some ( Delay :: new ( self . duration ) ) ;
54+ this . delay . set ( Some ( Delay :: new ( * this . duration ) ) ) ;
5555 Poll :: Ready ( Some ( v) )
5656 }
5757 }
0 commit comments