diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-08-03 14:15:16 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-08-04 23:18:45 +0200 |
| commit | ac1a26b40f417e776de8ebd731f811134f97b3f9 (patch) | |
| tree | 43e712926ab879c95ff03e6c1eb3c91722c77536 /embassy-util/src | |
| parent | 3967c4194b1f28d2179fd30ec7fa688bcf23ab1b (diff) | |
util/pipe: add embedded-io impls for Pipe, Reader, Writer.
Diffstat (limited to 'embassy-util/src')
| -rw-r--r-- | embassy-util/src/lib.rs | 1 | ||||
| -rw-r--r-- | embassy-util/src/pipe.rs | 109 |
2 files changed, 109 insertions, 1 deletions
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")] | ||
| 323 | mod 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)] |
| 323 | mod tests { | 432 | mod tests { |
| 324 | use futures_executor::ThreadPool; | 433 | use futures_executor::ThreadPool; |
