Skip to content

Commit

Permalink
io-adapters: add support for BufRead for futures and tokio.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirbaio committed Aug 25, 2023
1 parent 35e6012 commit 2c9db0d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
4 changes: 4 additions & 0 deletions embedded-io-adapters/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

- Add support for adapting `BufRead` from `futures` and `tokio`.

## 0.5.0 - 2023-08-06

- First release
2 changes: 1 addition & 1 deletion embedded-io-adapters/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ embedded-io = { version = "0.5", path = "../embedded-io" }
embedded-io-async = { version = "0.5", path = "../embedded-io-async", optional = true }

futures = { version = "0.3.21", features = ["std"], default-features = false, optional = true }
tokio = { version = "1", default-features = false, optional = true }
tokio = { version = "1", features = ["io-util"], default-features = false, optional = true }

[package.metadata.docs.rs]
features = ["std", "tokio-1", "futures-03"]
Expand Down
12 changes: 12 additions & 0 deletions embedded-io-adapters/src/futures_03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use core::future::poll_fn;
use core::pin::Pin;

use futures::AsyncBufReadExt;

/// Adapter from `futures::io` traits.
#[derive(Clone)]
pub struct FromFutures<T: ?Sized> {
Expand Down Expand Up @@ -43,6 +45,16 @@ impl<T: futures::io::AsyncRead + Unpin + ?Sized> embedded_io_async::Read for Fro
}
}

impl<T: futures::io::AsyncBufRead + Unpin + ?Sized> embedded_io_async::BufRead for FromFutures<T> {
async fn fill_buf(&mut self) -> Result<&[u8], Self::Error> {
self.inner.fill_buf().await
}

fn consume(&mut self, amt: usize) {
Pin::new(&mut self.inner).consume(amt)
}
}

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
Expand Down
12 changes: 12 additions & 0 deletions embedded-io-adapters/src/tokio_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use core::future::poll_fn;
use core::pin::Pin;
use core::task::Poll;

use tokio::io::AsyncBufReadExt;

/// Adapter from `tokio::io` traits.
#[derive(Clone)]
pub struct FromTokio<T: ?Sized> {
Expand Down Expand Up @@ -54,6 +56,16 @@ impl<T: tokio::io::AsyncRead + Unpin + ?Sized> embedded_io_async::Read for FromT
}
}

impl<T: tokio::io::AsyncBufRead + Unpin + ?Sized> embedded_io_async::BufRead for FromTokio<T> {
async fn fill_buf(&mut self) -> Result<&[u8], Self::Error> {
self.inner.fill_buf().await
}

fn consume(&mut self, amt: usize) {
Pin::new(&mut self.inner).consume(amt)
}
}

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
Expand Down

0 comments on commit 2c9db0d

Please sign in to comment.