Skip to content

Commit ef958f0

Browse files
committed
Use pin_project_lite instead for throttle
1 parent 139a34b commit ef958f0

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

src/stream/stream/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,6 @@ extension_trait! {
315315
TakeWhile::new(self, predicate)
316316
}
317317

318-
#[cfg(all(feature = "default", feature = "unstable"))]
319318
#[doc = r#"
320319
Limit the amount of items yielded per timeslice in a stream.
321320
@@ -342,6 +341,8 @@ extension_trait! {
342341
# }) }
343342
```
344343
"#]
344+
#[cfg(all(feature = "default", feature = "unstable"))]
345+
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
345346
fn throttle(self, d: Duration) -> Throttle<Self>
346347
where
347348
Self: Sized,

src/stream/stream/throttle.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,25 @@ use std::pin::Pin;
33
use std::time::Duration;
44

55
use futures_timer::Delay;
6+
use pin_project_lite::pin_project;
67

78
use crate::stream::Stream;
89
use 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-
2124
impl<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> {
3534
impl<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

Comments
 (0)