Skip to content

Commit 5d406a4

Browse files
authored
Merge pull request #489 from Dirbaio/io-adapter-buf
io-adapters: add support for BufRead for `futures` and `tokio`.
2 parents 35e6012 + 2c9db0d commit 5d406a4

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

embedded-io-adapters/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## Unreleased
9+
10+
- Add support for adapting `BufRead` from `futures` and `tokio`.
11+
812
## 0.5.0 - 2023-08-06
913

1014
- First release

embedded-io-adapters/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ embedded-io = { version = "0.5", path = "../embedded-io" }
2121
embedded-io-async = { version = "0.5", path = "../embedded-io-async", optional = true }
2222

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

2626
[package.metadata.docs.rs]
2727
features = ["std", "tokio-1", "futures-03"]

embedded-io-adapters/src/futures_03.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
use core::future::poll_fn;
44
use core::pin::Pin;
55

6+
use futures::AsyncBufReadExt;
7+
68
/// Adapter from `futures::io` traits.
79
#[derive(Clone)]
810
pub struct FromFutures<T: ?Sized> {
@@ -43,6 +45,16 @@ impl<T: futures::io::AsyncRead + Unpin + ?Sized> embedded_io_async::Read for Fro
4345
}
4446
}
4547

48+
impl<T: futures::io::AsyncBufRead + Unpin + ?Sized> embedded_io_async::BufRead for FromFutures<T> {
49+
async fn fill_buf(&mut self) -> Result<&[u8], Self::Error> {
50+
self.inner.fill_buf().await
51+
}
52+
53+
fn consume(&mut self, amt: usize) {
54+
Pin::new(&mut self.inner).consume(amt)
55+
}
56+
}
57+
4658
impl<T: futures::io::AsyncWrite + Unpin + ?Sized> embedded_io_async::Write for FromFutures<T> {
4759
async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
4860
poll_fn(|cx| Pin::new(&mut self.inner).poll_write(cx, buf)).await

embedded-io-adapters/src/tokio_1.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use core::future::poll_fn;
44
use core::pin::Pin;
55
use core::task::Poll;
66

7+
use tokio::io::AsyncBufReadExt;
8+
79
/// Adapter from `tokio::io` traits.
810
#[derive(Clone)]
911
pub struct FromTokio<T: ?Sized> {
@@ -54,6 +56,16 @@ impl<T: tokio::io::AsyncRead + Unpin + ?Sized> embedded_io_async::Read for FromT
5456
}
5557
}
5658

59+
impl<T: tokio::io::AsyncBufRead + Unpin + ?Sized> embedded_io_async::BufRead for FromTokio<T> {
60+
async fn fill_buf(&mut self) -> Result<&[u8], Self::Error> {
61+
self.inner.fill_buf().await
62+
}
63+
64+
fn consume(&mut self, amt: usize) {
65+
Pin::new(&mut self.inner).consume(amt)
66+
}
67+
}
68+
5769
impl<T: tokio::io::AsyncWrite + Unpin + ?Sized> embedded_io_async::Write for FromTokio<T> {
5870
async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
5971
poll_fn(|cx| Pin::new(&mut self.inner).poll_write(cx, buf)).await

0 commit comments

Comments
 (0)