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

Handle reading from Tokio with empty buf as a special case #519

Merged
merged 2 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions embedded-io-adapters/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

Add unreleased changes here

- Handle reading from `FromTokio` with empty buffer, ensuring `Ok(0)` is always returned.

## 0.6.0 - 2023-10-02

- Add support for adapting `BufRead` from `futures` and `tokio`.
Expand Down
8 changes: 8 additions & 0 deletions embedded-io-adapters/src/tokio_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ impl<T: ?Sized> embedded_io::ErrorType for FromTokio<T> {

impl<T: tokio::io::AsyncRead + Unpin + ?Sized> embedded_io_async::Read for FromTokio<T> {
async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
// The current tokio implementation (https://github.com/tokio-rs/tokio/blob/tokio-1.33.0/tokio/src/io/poll_evented.rs#L165)
// does not consider the case of buf.is_empty() as a special case,
// which can cause Poll::Pending to be returned at the end of the stream when called with an empty buffer.
// This poll will, however, never become ready, as no more bytes will be received.
if buf.is_empty() {
return Ok(0);
}

poll_fn(|cx| {
let mut buf = tokio::io::ReadBuf::new(buf);
match Pin::new(&mut self.inner).poll_read(cx, &mut buf) {
Expand Down