msrt_uart/traits.rs
1//! UART IO traits shared by std and no-std adapters.
2
3/// Result alias for UART IO operations.
4pub type UartIoResult<T, E> = core::result::Result<T, UartIoError<E>>;
5
6/// Result alias for UART adapter operations.
7pub type UartResult<T, E> = core::result::Result<T, UartError<E>>;
8
9/// UART adapter error wrapper.
10#[derive(Clone, Copy, Debug, Eq, PartialEq)]
11pub enum UartError<E> {
12 /// UART transport failed.
13 Io(UartIoError<E>),
14 /// MSRT protocol processing failed.
15 Protocol(msrt::error::Error),
16}
17
18/// UART transport error wrapper.
19#[derive(Clone, Copy, Debug, Eq, PartialEq)]
20pub enum UartIoError<E> {
21 /// No bytes are currently available.
22 WouldBlock,
23 /// Operation was interrupted and may be retried.
24 Interrupted,
25 /// Transport-specific error.
26 Other(E),
27}
28
29/// Minimal UART byte-stream trait for MCU/no-std backends.
30///
31/// Implement this trait for a board UART, DMA ring, RTOS queue, or test double.
32pub trait UartIo {
33 /// Transport-specific error type.
34 type Error;
35
36 /// Reads currently available bytes into `buf`.
37 ///
38 /// Return `Ok(0)` for EOF-like idle streams, or `Err(WouldBlock)` when no
39 /// bytes are available right now.
40 fn read(&mut self, buf: &mut [u8]) -> UartIoResult<usize, Self::Error>;
41
42 /// Writes all bytes to the UART transport.
43 fn write_all(&mut self, bytes: &[u8]) -> UartIoResult<(), Self::Error>;
44}