Skip to content

Commit

Permalink
Update embedded-io-adapters to swap Ok(0) for Err when required
Browse files Browse the repository at this point in the history
  • Loading branch information
adamgreig committed Sep 13, 2023
1 parent 90e2c5e commit 9d40f6b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
4 changes: 3 additions & 1 deletion embedded-io-adapters/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased

- Add support for adapting `BufRead` from `futures` and `tokio`.
- Return an error when a wrapped `std`/`futures`/`tokio` `write()` call returns
`Ok(0)` to comply with `embedded_io::Write` requirements.

## 0.5.0 - 2023-08-06

- First release
- First release
6 changes: 5 additions & 1 deletion embedded-io-adapters/src/futures_03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ impl<T: futures::io::AsyncBufRead + Unpin + ?Sized> embedded_io_async::BufRead f

impl<T: futures::io::AsyncWrite + Unpin + ?Sized> embedded_io_async::Write for FromFutures<T> {
async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
poll_fn(|cx| Pin::new(&mut self.inner).poll_write(cx, buf)).await
match poll_fn(|cx| Pin::new(&mut self.inner).poll_write(cx, buf)).await {
Ok(0) if !buf.is_empty() => Err(std::io::ErrorKind::WriteZero.into()),
Ok(n) => Ok(n),
Err(e) => Err(e),
}
}

async fn flush(&mut self) -> Result<(), Self::Error> {
Expand Down
14 changes: 12 additions & 2 deletions embedded-io-adapters/src/std.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Adapters to/from `std::io` traits.
use embedded_io::Error;

/// Adapter from `std::io` traits.
#[derive(Clone)]
pub struct FromStd<T: ?Sized> {
Expand Down Expand Up @@ -52,7 +54,11 @@ impl<T: std::io::BufRead + ?Sized> embedded_io::BufRead for FromStd<T> {

impl<T: std::io::Write + ?Sized> embedded_io::Write for FromStd<T> {
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
self.inner.write(buf)
match self.inner.write(buf) {
Ok(0) if !buf.is_empty() => Err(std::io::ErrorKind::WriteZero.into()),
Ok(n) => Ok(n),
Err(e) => Err(e),
}
}
fn flush(&mut self) -> Result<(), Self::Error> {
self.inner.flush()
Expand Down Expand Up @@ -103,7 +109,11 @@ impl<T: embedded_io::Read + ?Sized> std::io::Read for ToStd<T> {

impl<T: embedded_io::Write + ?Sized> std::io::Write for ToStd<T> {
fn write(&mut self, buf: &[u8]) -> Result<usize, std::io::Error> {
self.inner.write(buf).map_err(to_std_error)
match self.inner.write(buf) {
Ok(n) => Ok(n),
Err(e) if e.kind() == embedded_io::ErrorKind::WriteZero => Ok(0),
Err(e) => Err(to_std_error(e)),
}
}
fn flush(&mut self) -> Result<(), std::io::Error> {
self.inner.flush().map_err(to_std_error)
Expand Down
6 changes: 5 additions & 1 deletion embedded-io-adapters/src/tokio_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ impl<T: tokio::io::AsyncBufRead + Unpin + ?Sized> embedded_io_async::BufRead for

impl<T: tokio::io::AsyncWrite + Unpin + ?Sized> embedded_io_async::Write for FromTokio<T> {
async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
poll_fn(|cx| Pin::new(&mut self.inner).poll_write(cx, buf)).await
match poll_fn(|cx| Pin::new(&mut self.inner).poll_write(cx, buf)).await {
Ok(0) if !buf.is_empty() => Err(std::io::ErrorKind::WriteZero.into()),
Ok(n) => Ok(n),
Err(e) => Err(e),
}
}

async fn flush(&mut self) -> Result<(), Self::Error> {
Expand Down

0 comments on commit 9d40f6b

Please sign in to comment.