From 2c9db0d682b37c931ffdd011d1c81cf2ebcc1976 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Fri, 25 Aug 2023 01:11:15 +0200 Subject: [PATCH] io-adapters: add support for BufRead for `futures` and `tokio`. --- embedded-io-adapters/CHANGELOG.md | 4 ++++ embedded-io-adapters/Cargo.toml | 2 +- embedded-io-adapters/src/futures_03.rs | 12 ++++++++++++ embedded-io-adapters/src/tokio_1.rs | 12 ++++++++++++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/embedded-io-adapters/CHANGELOG.md b/embedded-io-adapters/CHANGELOG.md index 978a92d8b..bf9f2d391 100644 --- a/embedded-io-adapters/CHANGELOG.md +++ b/embedded-io-adapters/CHANGELOG.md @@ -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 \ No newline at end of file diff --git a/embedded-io-adapters/Cargo.toml b/embedded-io-adapters/Cargo.toml index 5dbcf4354..a849b1f12 100644 --- a/embedded-io-adapters/Cargo.toml +++ b/embedded-io-adapters/Cargo.toml @@ -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"] diff --git a/embedded-io-adapters/src/futures_03.rs b/embedded-io-adapters/src/futures_03.rs index b672215ed..a3402cbb9 100644 --- a/embedded-io-adapters/src/futures_03.rs +++ b/embedded-io-adapters/src/futures_03.rs @@ -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 { @@ -43,6 +45,16 @@ impl embedded_io_async::Read for Fro } } +impl embedded_io_async::BufRead for FromFutures { + 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 embedded_io_async::Write for FromFutures { async fn write(&mut self, buf: &[u8]) -> Result { poll_fn(|cx| Pin::new(&mut self.inner).poll_write(cx, buf)).await diff --git a/embedded-io-adapters/src/tokio_1.rs b/embedded-io-adapters/src/tokio_1.rs index 04e1cf1aa..92060a4fe 100644 --- a/embedded-io-adapters/src/tokio_1.rs +++ b/embedded-io-adapters/src/tokio_1.rs @@ -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 { @@ -54,6 +56,16 @@ impl embedded_io_async::Read for FromT } } +impl embedded_io_async::BufRead for FromTokio { + 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 embedded_io_async::Write for FromTokio { async fn write(&mut self, buf: &[u8]) -> Result { poll_fn(|cx| Pin::new(&mut self.inner).poll_write(cx, buf)).await