aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2022-08-03 14:15:16 +0200
committerDario Nieuwenhuis <[email protected]>2022-08-04 23:18:45 +0200
commitac1a26b40f417e776de8ebd731f811134f97b3f9 (patch)
tree43e712926ab879c95ff03e6c1eb3c91722c77536
parent3967c4194b1f28d2179fd30ec7fa688bcf23ab1b (diff)
util/pipe: add embedded-io impls for Pipe, Reader, Writer.
-rw-r--r--embassy-util/Cargo.toml6
-rw-r--r--embassy-util/src/lib.rs1
-rw-r--r--embassy-util/src/pipe.rs109
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"
6[package.metadata.embassy_docs] 6[package.metadata.embassy_docs]
7src_base = "https://github.com/embassy-rs/embassy/blob/embassy-util-v$VERSION/embassy-util/src/" 7src_base = "https://github.com/embassy-rs/embassy/blob/embassy-util-v$VERSION/embassy-util/src/"
8src_base_git = "https://github.com/embassy-rs/embassy/blob/$COMMIT/embassy-util/src/" 8src_base_git = "https://github.com/embassy-rs/embassy/blob/$COMMIT/embassy-util/src/"
9features = ["nightly", "defmt", "unstable-traits", "time", "time-tick-1mhz"] 9features = ["nightly"]
10flavors = [ 10flavors = [
11 { name = "default", target = "x86_64-unknown-linux-gnu" }, 11 { name = "default", target = "x86_64-unknown-linux-gnu" },
12] 12]
13 13
14[features]
15nightly = ["embedded-io/async"]
16
14[dependencies] 17[dependencies]
15defmt = { version = "0.3", optional = true } 18defmt = { version = "0.3", optional = true }
16log = { version = "0.4.14", optional = true } 19log = { version = "0.4.14", optional = true }
@@ -20,6 +23,7 @@ atomic-polyfill = "0.1.5"
20critical-section = "0.2.5" 23critical-section = "0.2.5"
21heapless = "0.7.5" 24heapless = "0.7.5"
22cfg-if = "1.0.0" 25cfg-if = "1.0.0"
26embedded-io = "0.3.0"
23 27
24[dev-dependencies] 28[dev-dependencies]
25futures-executor = { version = "0.3.17", features = [ "thread-pool" ] } 29futures-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 @@
1#![cfg_attr(not(any(feature = "std", feature = "wasm")), no_std)] 1#![cfg_attr(not(any(feature = "std", feature = "wasm")), no_std)]
2#![cfg_attr(feature = "nightly", feature(generic_associated_types, type_alias_impl_trait))] 2#![cfg_attr(feature = "nightly", feature(generic_associated_types, type_alias_impl_trait))]
3#![cfg_attr(all(feature = "nightly", target_arch = "xtensa"), feature(asm_experimental_arch))]
4#![allow(clippy::new_without_default)] 3#![allow(clippy::new_without_default)]
5#![doc = include_str!("../../README.md")] 4#![doc = include_str!("../../README.md")]
6#![warn(missing_docs)] 5#![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
319 } 319 }
320} 320}
321 321
322#[cfg(feature = "nightly")]
323mod io_impls {
324 use core::convert::Infallible;
325
326 use futures_util::FutureExt;
327
328 use super::*;
329
330 impl<M: RawMutex, const N: usize> embedded_io::Io for Pipe<M, N> {
331 type Error = Infallible;
332 }
333
334 impl<M: RawMutex, const N: usize> embedded_io::asynch::Read for Pipe<M, N> {
335 type ReadFuture<'a> = impl Future<Output = Result<usize, Self::Error>>
336 where
337 Self: 'a;
338
339 fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> {
340 Pipe::read(self, buf).map(Ok)
341 }
342 }
343
344 impl<M: RawMutex, const N: usize> embedded_io::asynch::Write for Pipe<M, N> {
345 type WriteFuture<'a> = impl Future<Output = Result<usize, Self::Error>>
346 where
347 Self: 'a;
348
349 fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> {
350 Pipe::write(self, buf).map(Ok)
351 }
352
353 type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>>
354 where
355 Self: 'a;
356
357 fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> {
358 futures_util::future::ready(Ok(()))
359 }
360 }
361
362 impl<M: RawMutex, const N: usize> embedded_io::Io for &Pipe<M, N> {
363 type Error = Infallible;
364 }
365
366 impl<M: RawMutex, const N: usize> embedded_io::asynch::Read for &Pipe<M, N> {
367 type ReadFuture<'a> = impl Future<Output = Result<usize, Self::Error>>
368 where
369 Self: 'a;
370
371 fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> {
372 Pipe::read(self, buf).map(Ok)
373 }
374 }
375
376 impl<M: RawMutex, const N: usize> embedded_io::asynch::Write for &Pipe<M, N> {
377 type WriteFuture<'a> = impl Future<Output = Result<usize, Self::Error>>
378 where
379 Self: 'a;
380
381 fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> {
382 Pipe::write(self, buf).map(Ok)
383 }
384
385 type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>>
386 where
387 Self: 'a;
388
389 fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> {
390 futures_util::future::ready(Ok(()))
391 }
392 }
393
394 impl<M: RawMutex, const N: usize> embedded_io::Io for Reader<'_, M, N> {
395 type Error = Infallible;
396 }
397
398 impl<M: RawMutex, const N: usize> embedded_io::asynch::Read for Reader<'_, M, N> {
399 type ReadFuture<'a> = impl Future<Output = Result<usize, Self::Error>>
400 where
401 Self: 'a;
402
403 fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> {
404 Reader::read(self, buf).map(Ok)
405 }
406 }
407
408 impl<M: RawMutex, const N: usize> embedded_io::Io for Writer<'_, M, N> {
409 type Error = Infallible;
410 }
411
412 impl<M: RawMutex, const N: usize> embedded_io::asynch::Write for Writer<'_, M, N> {
413 type WriteFuture<'a> = impl Future<Output = Result<usize, Self::Error>>
414 where
415 Self: 'a;
416
417 fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> {
418 Writer::write(self, buf).map(Ok)
419 }
420
421 type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>>
422 where
423 Self: 'a;
424
425 fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> {
426 futures_util::future::ready(Ok(()))
427 }
428 }
429}
430
322#[cfg(test)] 431#[cfg(test)]
323mod tests { 432mod tests {
324 use futures_executor::ThreadPool; 433 use futures_executor::ThreadPool;