From ac1a26b40f417e776de8ebd731f811134f97b3f9 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Wed, 3 Aug 2022 14:15:16 +0200 Subject: [PATCH] util/pipe: add embedded-io impls for Pipe, Reader, Writer. --- embassy-util/Cargo.toml | 6 ++- embassy-util/src/lib.rs | 1 - embassy-util/src/pipe.rs | 109 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 2 deletions(-) diff --git a/embassy-util/Cargo.toml b/embassy-util/Cargo.toml index 32b796c0a..ef5acc0f0 100644 --- a/embassy-util/Cargo.toml +++ b/embassy-util/Cargo.toml @@ -6,11 +6,14 @@ edition = "2021" [package.metadata.embassy_docs] src_base = "https://github.com/embassy-rs/embassy/blob/embassy-util-v$VERSION/embassy-util/src/" src_base_git = "https://github.com/embassy-rs/embassy/blob/$COMMIT/embassy-util/src/" -features = ["nightly", "defmt", "unstable-traits", "time", "time-tick-1mhz"] +features = ["nightly"] flavors = [ { name = "default", target = "x86_64-unknown-linux-gnu" }, ] +[features] +nightly = ["embedded-io/async"] + [dependencies] defmt = { version = "0.3", optional = true } log = { version = "0.4.14", optional = true } @@ -20,6 +23,7 @@ atomic-polyfill = "0.1.5" critical-section = "0.2.5" heapless = "0.7.5" cfg-if = "1.0.0" +embedded-io = "0.3.0" [dev-dependencies] futures-executor = { version = "0.3.17", features = [ "thread-pool" ] } diff --git a/embassy-util/src/lib.rs b/embassy-util/src/lib.rs index a65ebd518..110c72811 100644 --- a/embassy-util/src/lib.rs +++ b/embassy-util/src/lib.rs @@ -1,6 +1,5 @@ #![cfg_attr(not(any(feature = "std", feature = "wasm")), no_std)] #![cfg_attr(feature = "nightly", feature(generic_associated_types, type_alias_impl_trait))] -#![cfg_attr(all(feature = "nightly", target_arch = "xtensa"), feature(asm_experimental_arch))] #![allow(clippy::new_without_default)] #![doc = include_str!("../../README.md")] #![warn(missing_docs)] diff --git a/embassy-util/src/pipe.rs b/embassy-util/src/pipe.rs index e4f21732a..c0a5d2f6f 100644 --- a/embassy-util/src/pipe.rs +++ b/embassy-util/src/pipe.rs @@ -319,6 +319,115 @@ where } } +#[cfg(feature = "nightly")] +mod io_impls { + use core::convert::Infallible; + + use futures_util::FutureExt; + + use super::*; + + impl embedded_io::Io for Pipe { + type Error = Infallible; + } + + impl embedded_io::asynch::Read for Pipe { + type ReadFuture<'a> = impl Future> + where + Self: 'a; + + fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> { + Pipe::read(self, buf).map(Ok) + } + } + + impl embedded_io::asynch::Write for Pipe { + type WriteFuture<'a> = impl Future> + where + Self: 'a; + + fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> { + Pipe::write(self, buf).map(Ok) + } + + type FlushFuture<'a> = impl Future> + where + Self: 'a; + + fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> { + futures_util::future::ready(Ok(())) + } + } + + impl embedded_io::Io for &Pipe { + type Error = Infallible; + } + + impl embedded_io::asynch::Read for &Pipe { + type ReadFuture<'a> = impl Future> + where + Self: 'a; + + fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> { + Pipe::read(self, buf).map(Ok) + } + } + + impl embedded_io::asynch::Write for &Pipe { + type WriteFuture<'a> = impl Future> + where + Self: 'a; + + fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> { + Pipe::write(self, buf).map(Ok) + } + + type FlushFuture<'a> = impl Future> + where + Self: 'a; + + fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> { + futures_util::future::ready(Ok(())) + } + } + + impl embedded_io::Io for Reader<'_, M, N> { + type Error = Infallible; + } + + impl embedded_io::asynch::Read for Reader<'_, M, N> { + type ReadFuture<'a> = impl Future> + where + Self: 'a; + + fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> { + Reader::read(self, buf).map(Ok) + } + } + + impl embedded_io::Io for Writer<'_, M, N> { + type Error = Infallible; + } + + impl embedded_io::asynch::Write for Writer<'_, M, N> { + type WriteFuture<'a> = impl Future> + where + Self: 'a; + + fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> { + Writer::write(self, buf).map(Ok) + } + + type FlushFuture<'a> = impl Future> + where + Self: 'a; + + fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> { + futures_util::future::ready(Ok(())) + } + } +} + #[cfg(test)] mod tests { use futures_executor::ThreadPool;