Re-implemented Throttle to keep last value in memory

new-scheduler
Wouter Geraedts 5 years ago
parent ced5281b73
commit 1c843a8124

@ -281,7 +281,7 @@ extension_trait! {
TakeWhile::new(self, predicate) TakeWhile::new(self, predicate)
} }
fn throttle(self, d: Duration) -> Throttle<Self> fn throttle(self, d: Duration) -> Throttle<Self, Self::Item>
where where
Self: Sized, Self: Sized,
{ {

@ -10,47 +10,58 @@ use crate::task::{Context, Poll};
/// A stream that only yields one element once every `duration`, and drops all others. /// A stream that only yields one element once every `duration`, and drops all others.
/// #[doc(hidden)] /// #[doc(hidden)]
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
pub struct Throttle<S> { pub struct Throttle<S, T> {
stream: S, stream: S,
duration: Duration, duration: Duration,
delay: Option<Delay>, delay: Option<Delay>,
last: Option<T>,
} }
impl<S: Unpin> Unpin for Throttle<S> {} impl<S: Unpin, T> Unpin for Throttle<S, T> {}
impl<S: Stream> Throttle<S> { impl<S: Stream> Throttle<S, S::Item> {
pin_utils::unsafe_pinned!(stream: S); pin_utils::unsafe_pinned!(stream: S);
pin_utils::unsafe_unpinned!(duration: Duration); pin_utils::unsafe_unpinned!(duration: Duration);
pin_utils::unsafe_pinned!(delay: Option<Delay>); pin_utils::unsafe_pinned!(delay: Option<Delay>);
pin_utils::unsafe_unpinned!(last: Option<S::Item>);
pub(super) fn new(stream: S, duration: Duration) -> Self { pub(super) fn new(stream: S, duration: Duration) -> Self {
Throttle { Throttle {
stream, stream,
duration, duration,
delay: None, delay: None,
last: None,
} }
} }
} }
impl<S: Stream> Stream for Throttle<S> { impl<S: Stream> Stream for Throttle<S, S::Item> {
type Item = S::Item; type Item = S::Item;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<S::Item>> { fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<S::Item>> {
match self.as_mut().stream().poll_next(cx) { if let Some(d) = self.as_mut().delay().as_pin_mut() {
Poll::Ready(v) => match self.as_mut().delay().as_pin_mut() { if d.poll(cx).is_ready() {
None => { if let Some(v) = self.as_mut().last().take() {
// Sets last to None.
*self.as_mut().delay() = Some(Delay::new(self.duration)); *self.as_mut().delay() = Some(Delay::new(self.duration));
Poll::Ready(v) return Poll::Ready(Some(v));
} }
Some(d) => match d.poll(cx) { }
Poll::Ready(_) => { }
*self.as_mut().delay() = Some(Delay::new(self.duration));
Poll::Ready(v) match self.as_mut().stream().poll_next(cx) {
}
Poll::Pending => Poll::Pending,
},
},
Poll::Pending => Poll::Pending, Poll::Pending => Poll::Pending,
Poll::Ready(None) => return Poll::Ready(None),
Poll::Ready(Some(v)) => {
if self.as_mut().delay().is_some() {
*self.as_mut().last() = Some(v);
cx.waker().wake_by_ref(); // Continue driving even though emitting Pending
return Poll::Pending;
}
*self.as_mut().delay() = Some(Delay::new(self.duration));
Poll::Ready(Some(v))
}
} }
} }
} }

Loading…
Cancel
Save