embedded_hal_nb/
serial.rs1pub trait Error: core::fmt::Debug {
5 fn kind(&self) -> ErrorKind;
11}
12
13impl Error for core::convert::Infallible {
14 #[inline]
15 fn kind(&self) -> ErrorKind {
16 match *self {}
17 }
18}
19
20#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
26#[non_exhaustive]
27pub enum ErrorKind {
28 Overrun,
30 FrameFormat,
33 Parity,
35 Noise,
37 Other,
39}
40
41impl Error for ErrorKind {
42 #[inline]
43 fn kind(&self) -> ErrorKind {
44 *self
45 }
46}
47
48impl core::fmt::Display for ErrorKind {
49 #[inline]
50 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
51 match self {
52 Self::Overrun => write!(f, "The peripheral receive buffer was overrun"),
53 Self::Parity => write!(f, "Parity check failed"),
54 Self::Noise => write!(f, "Serial line is too noisy to read valid data"),
55 Self::FrameFormat => write!(
56 f,
57 "Received data does not conform to the peripheral configuration"
58 ),
59 Self::Other => write!(
60 f,
61 "A different error occurred. The original error may contain more information"
62 ),
63 }
64 }
65}
66
67pub trait ErrorType {
71 type Error: Error;
73}
74
75impl<T: ErrorType + ?Sized> ErrorType for &mut T {
76 type Error = T::Error;
77}
78
79pub trait Read<Word: Copy = u8>: ErrorType {
84 fn read(&mut self) -> nb::Result<Word, Self::Error>;
86}
87
88impl<T: Read<Word> + ?Sized, Word: Copy> Read<Word> for &mut T {
89 #[inline]
90 fn read(&mut self) -> nb::Result<Word, Self::Error> {
91 T::read(self)
92 }
93}
94
95pub trait Write<Word: Copy = u8>: ErrorType {
97 fn write(&mut self, word: Word) -> nb::Result<(), Self::Error>;
99
100 fn flush(&mut self) -> nb::Result<(), Self::Error>;
102}
103
104impl<T: Write<Word> + ?Sized, Word: Copy> Write<Word> for &mut T {
105 #[inline]
106 fn write(&mut self, word: Word) -> nb::Result<(), Self::Error> {
107 T::write(self, word)
108 }
109
110 #[inline]
111 fn flush(&mut self) -> nb::Result<(), Self::Error> {
112 T::flush(self)
113 }
114}
115
116impl<Word, Error: self::Error> core::fmt::Write for dyn Write<Word, Error = Error> + '_
121where
122 Word: Copy + From<u8>,
123{
124 #[inline]
125 fn write_str(&mut self, s: &str) -> core::fmt::Result {
126 let _ = s
127 .bytes()
128 .map(|c| nb::block!(self.write(Word::from(c))))
129 .last();
130 Ok(())
131 }
132}