Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid creating Delay multiple times #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
use futures::stream::{Fuse, FusedStream, Stream};
use futures::task::{Context, Poll};
use futures::Future;
use futures::StreamExt;

Check warning on line 38 in src/lib.rs

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, nightly)

unexpected `cfg` condition value: `sink`

Check warning on line 38 in src/lib.rs

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, nightly)

unexpected `cfg` condition value: `sink`

Check warning on line 38 in src/lib.rs

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, nightly)

unexpected `cfg` condition value: `sink`

Check warning on line 38 in src/lib.rs

View workflow job for this annotation

GitHub Actions / ci (windows-latest, nightly)

unexpected `cfg` condition value: `sink`

Check warning on line 38 in src/lib.rs

View workflow job for this annotation

GitHub Actions / ci (windows-latest, nightly)

unexpected `cfg` condition value: `sink`

Check warning on line 38 in src/lib.rs

View workflow job for this annotation

GitHub Actions / ci (windows-latest, nightly)

unexpected `cfg` condition value: `sink`

Check warning on line 38 in src/lib.rs

View workflow job for this annotation

GitHub Actions / ci (macOS-latest, nightly)

unexpected `cfg` condition value: `sink`

Check warning on line 38 in src/lib.rs

View workflow job for this annotation

GitHub Actions / ci (macOS-latest, nightly)

unexpected `cfg` condition value: `sink`

Check warning on line 38 in src/lib.rs

View workflow job for this annotation

GitHub Actions / ci (macOS-latest, nightly)

unexpected `cfg` condition value: `sink`
#[cfg(feature = "sink")]
use futures_sink::Sink;
use pin_utils::{unsafe_pinned, unsafe_unpinned};
Expand Down Expand Up @@ -64,6 +64,7 @@
cap: usize,
// https://github.com/rust-lang-nursery/futures-rs/issues/1475
clock: Option<Delay>,
clock_used: bool,
duration: Duration,
}

Expand All @@ -74,6 +75,7 @@
St: Stream,
{
unsafe_unpinned!(items: Vec<St::Item>);
unsafe_unpinned!(clock_used: bool);
unsafe_pinned!(clock: Option<Delay>);
unsafe_pinned!(stream: Fuse<St>);

Expand All @@ -85,6 +87,7 @@
items: Vec::with_capacity(capacity),
cap: capacity,
clock: None,
clock_used: false,
duration,
}
}
Expand Down Expand Up @@ -131,6 +134,9 @@
type Item = Vec<St::Item>;

fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let duration = self.duration;
let make_clock = move || Delay::new(duration);

loop {
match self.as_mut().stream().poll_next(cx) {
Poll::Ready(item) => match item {
Expand All @@ -139,11 +145,15 @@
// the full one.
Some(item) => {
if self.items.is_empty() {
*self.as_mut().clock() = Some(Delay::new(self.duration));
*self.as_mut().clock_used() = true;
self.as_mut()
.clock()
.get_or_insert_with(make_clock)
.reset(duration);
}
self.as_mut().items().push(item);
if self.items.len() >= self.cap {
*self.as_mut().clock() = None;
*self.as_mut().clock_used() = false;
return Poll::Ready(Some(self.as_mut().take()));
} else {
// Continue the loop
Expand All @@ -168,26 +178,22 @@
Poll::Pending => {}
}

match self
.as_mut()
.clock()
.as_pin_mut()
.map(|clock| clock.poll(cx))
{
Some(Poll::Ready(())) => {
*self.as_mut().clock() = None;
if !self.clock_used {
debug_assert!(
self.items().is_empty(),
"Inner buffer is empty, but clock is available."
);
return Poll::Pending;
}

let clock = self.as_mut().clock().get_mut().get_or_insert_with(make_clock);
match Pin::new(clock).poll(cx) {
Poll::Ready(()) => {
*self.as_mut().clock_used() = false;
return Poll::Ready(Some(self.as_mut().take()));
}
Some(Poll::Pending) => {}
None => {
debug_assert!(
self.items().is_empty(),
"Inner buffer is empty, but clock is available."
);
}
Poll::Pending => return Poll::Pending,
}

return Poll::Pending;
}
}

Expand All @@ -209,7 +215,7 @@
}
}

// Forwarding impl of Sink from the underlying stream

Check warning on line 218 in src/lib.rs

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, nightly)

unexpected `cfg` condition value: `sink`

Check warning on line 218 in src/lib.rs

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, nightly)

unexpected `cfg` condition value: `sink`

Check warning on line 218 in src/lib.rs

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, nightly)

unexpected `cfg` condition value: `sink`

Check warning on line 218 in src/lib.rs

View workflow job for this annotation

GitHub Actions / ci (windows-latest, nightly)

unexpected `cfg` condition value: `sink`

Check warning on line 218 in src/lib.rs

View workflow job for this annotation

GitHub Actions / ci (windows-latest, nightly)

unexpected `cfg` condition value: `sink`

Check warning on line 218 in src/lib.rs

View workflow job for this annotation

GitHub Actions / ci (windows-latest, nightly)

unexpected `cfg` condition value: `sink`

Check warning on line 218 in src/lib.rs

View workflow job for this annotation

GitHub Actions / ci (macOS-latest, nightly)

unexpected `cfg` condition value: `sink`

Check warning on line 218 in src/lib.rs

View workflow job for this annotation

GitHub Actions / ci (macOS-latest, nightly)

unexpected `cfg` condition value: `sink`

Check warning on line 218 in src/lib.rs

View workflow job for this annotation

GitHub Actions / ci (macOS-latest, nightly)

unexpected `cfg` condition value: `sink`
#[cfg(feature = "sink")]
impl<S, Item> Sink<Item> for ChunksTimeout<S>
where
Expand Down
Loading