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

fix: drop completed futures in Zip and TryZip #106

Merged
merged 2 commits into from
Aug 25, 2024
Merged
Changes from 1 commit
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
48 changes: 28 additions & 20 deletions src/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ where
F2: Future,
{
Zip {
future1,
future2,
future1: Some(future1),
future2: Some(future2),
output1: None,
output2: None,
}
Expand All @@ -276,10 +276,10 @@ pin_project! {
F2: Future,
{
#[pin]
future1: F1,
future1: Option<F1>,
output1: Option<F1::Output>,
#[pin]
future2: F2,
future2: Option<F2>,
output2: Option<F2::Output>,
}
}
Expand All @@ -304,17 +304,19 @@ where
type Output = (F1::Output, F2::Output);

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
let mut this = self.project();

if this.output1.is_none() {
if let Poll::Ready(out) = this.future1.poll(cx) {
if let Some(future) = this.future1.as_mut().as_pin_mut() {
if let Poll::Ready(out) = future.poll(cx) {
*this.output1 = Some(out);
this.future1.set(None);
}
}

if this.output2.is_none() {
if let Poll::Ready(out) = this.future2.poll(cx) {
if let Some(future) = this.future2.as_mut().as_pin_mut() {
if let Poll::Ready(out) = future.poll(cx) {
*this.output2 = Some(out);
this.future2.set(None);
}
}

Expand Down Expand Up @@ -342,8 +344,8 @@ where
F2: Future<Output = Result<T2, E>>,
{
TryZip {
future1,
future2,
future1: Some(future1),
future2: Some(future2),
output1: None,
output2: None,
}
Expand All @@ -355,10 +357,10 @@ pin_project! {
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct TryZip<F1, T1, F2, T2> {
#[pin]
future1: F1,
future1: Option<F1>,
output1: Option<T1>,
#[pin]
future2: F2,
future2: Option<F2>,
output2: Option<T2>,
}
}
Expand All @@ -371,21 +373,27 @@ where
type Output = Result<(T1, T2), E>;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
let mut this = self.project();

if this.output1.is_none() {
if let Poll::Ready(out) = this.future1.poll(cx) {
if let Some(future) = this.future1.as_mut().as_pin_mut() {
if let Poll::Ready(out) = future.poll(cx) {
match out {
Ok(t) => *this.output1 = Some(t),
Ok(t) => {
*this.output1 = Some(t);
this.future1.set(None);
}
Err(err) => return Poll::Ready(Err(err)),
}
}
}

if this.output2.is_none() {
if let Poll::Ready(out) = this.future2.poll(cx) {
if let Some(future) = this.future2.as_mut().as_pin_mut() {
if let Poll::Ready(out) = future.poll(cx) {
match out {
Ok(t) => *this.output2 = Some(t),
Ok(t) => {
*this.output2 = Some(t);
this.future2.set(None);
}
Err(err) => return Poll::Ready(Err(err)),
}
}
Expand Down
Loading