Skip to content
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
38 changes: 29 additions & 9 deletions futures-util/src/sink/with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pin_project! {
f: F,
#[pin]
state: Option<Fut>,
item: Option<Item>,
_phantom: PhantomData<fn(U) -> Item>,
}
}
Expand All @@ -25,9 +26,14 @@ impl<Si, Item, U, Fut, F> fmt::Debug for With<Si, Item, U, Fut, F>
where
Si: fmt::Debug,
Fut: fmt::Debug,
Item: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("With").field("sink", &self.sink).field("state", &self.state).finish()
f.debug_struct("With")
.field("sink", &self.sink)
.field("state", &self.state)
.field("item", &self.item)
.finish()
}
}

Expand All @@ -42,13 +48,14 @@ where
Fut: Future<Output = Result<Item, E>>,
E: From<Si::Error>,
{
Self { state: None, sink, f, _phantom: PhantomData }
Self { state: None, sink, f, item: None, _phantom: PhantomData }
}
}

impl<Si, Item, U, Fut, F> Clone for With<Si, Item, U, Fut, F>
where
Si: Clone,
Item: Clone,
F: Clone,
Fut: Clone,
{
Expand All @@ -57,6 +64,7 @@ where
state: self.state.clone(),
sink: self.sink.clone(),
f: self.f.clone(),
item: self.item.clone(),
_phantom: PhantomData,
}
}
Expand Down Expand Up @@ -98,13 +106,25 @@ where
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), E>> {
let mut this = self.project();

let item = match this.state.as_mut().as_pin_mut() {
None => return Poll::Ready(Ok(())),
Some(fut) => ready!(fut.poll(cx))?,
};
this.state.set(None);
this.sink.start_send(item)?;
Poll::Ready(Ok(()))
loop {
if this.item.is_some() {
// Check if the underlying sink is prepared for another item.
// If it is, we have to send it without yielding in between.
match this.sink.as_mut().poll_ready(cx)? {
Poll::Ready(()) => this.sink.start_send(this.item.take().unwrap())?,
Poll::Pending => match this.sink.as_mut().poll_flush(cx)? {
Poll::Ready(()) => continue, // check `poll_ready` again
Poll::Pending => return Poll::Pending,
},
}
}
if let Some(fut) = this.state.as_mut().as_pin_mut() {
let item = ready!(fut.poll(cx))?;
*this.item = Some(item);
this.state.set(None);
}
return Poll::Ready(Ok(()));
}
}
}

Expand Down
Loading