Implement futures::Sink#215
Open
Ali-Mirghasemi wants to merge 6 commits into
Open
Conversation
elenaf9
reviewed
Dec 8, 2025
elenaf9
left a comment
Member
There was a problem hiding this comment.
Thanks! Couple of comments below.
Could you please also add a test for this?
Comment on lines
+367
to
+371
| match <Stream as AsyncWrite>::poll_flush(self, cx) { | ||
| Poll::Ready(Ok(())) => Poll::Ready(Ok(())), | ||
| Poll::Ready(Err(e)) => Poll::Ready(Err(e)), | ||
| Poll::Pending => Poll::Pending, | ||
| } |
Member
There was a problem hiding this comment.
Suggested change
| match <Stream as AsyncWrite>::poll_flush(self, cx) { | |
| Poll::Ready(Ok(())) => Poll::Ready(Ok(())), | |
| Poll::Ready(Err(e)) => Poll::Ready(Err(e)), | |
| Poll::Pending => Poll::Pending, | |
| } | |
| <Stream as AsyncWrite>::poll_flush(self, cx) |
The match block doesn't change any types, does it?
Author
There was a problem hiding this comment.
Yeah it's better without match block, i'll change it
Comment on lines
+375
to
+379
| match <Stream as AsyncWrite>::poll_close(self, cx) { | ||
| Poll::Ready(Ok(())) => Poll::Ready(Ok(())), | ||
| Poll::Ready(Err(e)) => Poll::Ready(Err(e)), | ||
| Poll::Pending => Poll::Pending, | ||
| } |
Member
There was a problem hiding this comment.
Suggested change
| match <Stream as AsyncWrite>::poll_close(self, cx) { | |
| Poll::Ready(Ok(())) => Poll::Ready(Ok(())), | |
| Poll::Ready(Err(e)) => Poll::Ready(Err(e)), | |
| Poll::Pending => Poll::Pending, | |
| } | |
| <Stream as AsyncWrite>::poll_close(self, cx) |
Same here.
Comment on lines
+310
to
+364
| fn poll_ready(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
| // Ensure the outgoing mpsc is ready. | ||
| ready!(self | ||
| .sender | ||
| .poll_ready(cx) | ||
| .map_err(|_| self.write_zero_err())?) | ||
| ; | ||
|
|
||
| let mut shared = self.shared(); | ||
|
|
||
| // If we cannot write any more (stream closed for writing) -> error. | ||
| if !shared.state().can_write() { | ||
| return Poll::Ready(Err(self.write_zero_err())); | ||
| } | ||
|
|
||
| // If no send credit, arrange to be woken when credit arrives and return Pending. | ||
| if shared.send_window() == 0 { | ||
| shared.writer = Some(cx.waker().clone()); | ||
| return Poll::Pending; | ||
| } | ||
|
|
||
| Poll::Ready(Ok(())) | ||
| } | ||
|
|
||
| fn start_send(mut self: Pin<&mut Self>, item: Packet) -> Result<(), Self::Error> { | ||
| let body = item.0; | ||
|
|
||
| // Limit the mutex guard scope | ||
| { | ||
| let mut shared = self.shared(); | ||
| if !shared.state().can_write() { | ||
| return Err(self.write_zero_err()); | ||
| } | ||
| if shared.send_window() == 0 { | ||
| return Err(io::Error::new( | ||
| io::ErrorKind::WouldBlock, | ||
| "no credit" | ||
| )); | ||
| } | ||
| let k = std::cmp::min( | ||
| shared.send_window(), | ||
| body.len().try_into().unwrap_or(u32::MAX), | ||
| ); | ||
| shared.consume_send_window(k); | ||
| } // guard dropped | ||
|
|
||
| // Now we can mutate self | ||
| let mut frame = Frame::data(self.id, body).unwrap().left(); | ||
| self.add_flag(frame.header_mut()); | ||
|
|
||
| let cmd = StreamCommand::SendFrame(frame); | ||
| self.sender | ||
| .start_send(cmd) | ||
| .map_err(|_| self.write_zero_err()) | ||
| } |
Member
There was a problem hiding this comment.
Can we extract common logic from here and <Stream as AsyncWrite>::poll_write into separate functions so that we don't duplicate any logic?
Author
There was a problem hiding this comment.
i didn't want to change too much, yeah i try to merge AsyncWrite and Sink logic together
Author
|
Can you review latest commits please |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds an implementation of the futures::Sink trait for the Stream struct, enabling native async stream support.
What’s Included
Benefits
This change allows Stream to be used directly in async contexts and makes it interoperable with the broader async ecosystem, especially within Actix-based applications.
Testing