Skip to main content

ove/
embedded_io_async_impl.rs

1// Copyright (C) 2026 Kamil Lulko <kamil.lulko@gmail.com>
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4//
5// This file is part of oveRTOS.
6
7//! `embedded-io-async` 0.7 trait impls.
8//!
9//! Compiled when both the `embedded-io-async` and `async` Cargo
10//! features are enabled. Provides `Read` on the async wrappers
11//! ([`AsyncUart`], [`AsyncStream`]) so any `embedded-io-async`-aware
12//! crate (line readers, codec drivers, async-MQTT clients) composes
13//! with oveRTOS I/O.
14//!
15//! `Read::read` takes `&mut self`; our wrappers have `&'static self`
16//! receivers. The impl is therefore on `&'static T` — `&mut &'static T`
17//! satisfies the trait's `&mut self` signature without imposing
18//! exterior mutability on the wrapper itself.
19//!
20//! Error mapping shares the sync `embedded_io_impl` via the
21//! `ErrorType` trait, which `embedded_io_async::Read` extends.
22
23use crate::error::Error;
24
25// ---------------------------------------------------------------------------
26// AsyncUart
27// ---------------------------------------------------------------------------
28
29#[cfg(has_uart)]
30mod uart_impl {
31    use super::*;
32    use crate::async_runtime::AsyncUart;
33
34    impl embedded_io::ErrorType for &'static AsyncUart {
35        type Error = Error;
36    }
37
38    impl embedded_io_async::Read for &'static AsyncUart {
39        async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
40            AsyncUart::read(*self, buf).await
41        }
42    }
43}
44
45// ---------------------------------------------------------------------------
46// AsyncStream<N>
47// ---------------------------------------------------------------------------
48
49#[cfg(has_stream)]
50mod stream_impl {
51    use super::*;
52    use crate::async_runtime::AsyncStream;
53
54    impl<const N: usize> embedded_io::ErrorType for &'static AsyncStream<N> {
55        type Error = Error;
56    }
57
58    impl<const N: usize> embedded_io_async::Read for &'static AsyncStream<N> {
59        async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
60            AsyncStream::read(*self, buf).await
61        }
62    }
63}