-
Notifications
You must be signed in to change notification settings - Fork 226
feat: add impl AsyncRead for ResponseDataStream #410
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -119,6 +119,29 @@ impl fmt::Display for ResponseData { | |||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #[cfg(feature = "with-tokio")] | ||||||||||||||
| impl tokio::io::AsyncRead for ResponseDataStream { | ||||||||||||||
| fn poll_read( | ||||||||||||||
| self: Pin<&mut Self>, | ||||||||||||||
| cx: &mut std::task::Context<'_>, | ||||||||||||||
| buf: &mut tokio::io::ReadBuf<'_>, | ||||||||||||||
| ) -> std::task::Poll<std::io::Result<()>> { | ||||||||||||||
| use futures::StreamExt; | ||||||||||||||
| let bytes = self.get_mut().bytes(); | ||||||||||||||
| match bytes.poll_next_unpin(cx) { | ||||||||||||||
| std::task::Poll::Ready(Some(Ok(chunk))) => { | ||||||||||||||
| buf.put_slice(&chunk); // Put the chunk into the buffer | ||||||||||||||
| std::task::Poll::Ready(Ok(())) | ||||||||||||||
| } | ||||||||||||||
| std::task::Poll::Ready(Some(Err(error))) => { | ||||||||||||||
| std::task::Poll::Ready(Err(std::io::Error::new(std::io::ErrorKind::Other, error))) | ||||||||||||||
| } | ||||||||||||||
| std::task::Poll::Ready(None) => std::task::Poll::Ready(Ok(())), | ||||||||||||||
|
||||||||||||||
| std::task::Poll::Ready(None) => std::task::Poll::Ready(Ok(())), | |
| // If no bytes were written to the buffer in this poll, signal EOF by returning Ok(()) | |
| std::task::Poll::Ready(None) => { | |
| // If the buffer was not filled in this poll, this signals EOF (0 bytes read) | |
| std::task::Poll::Ready(Ok(())) | |
| }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation doesn't check if the chunk size exceeds the buffer's remaining capacity. This could cause a panic if the chunk is larger than the available buffer space. Consider using
buf.remaining()to check capacity and handle partial writes appropriately.