pub trait Error: core::fmt::Debug {
fn kind(&self) -> ErrorKind;
}
impl Error for core::convert::Infallible {
#[inline]
fn kind(&self) -> ErrorKind {
match *self {}
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[non_exhaustive]
pub enum ErrorKind {
Overrun,
FrameFormat,
Parity,
Noise,
Other,
}
impl Error for ErrorKind {
#[inline]
fn kind(&self) -> ErrorKind {
*self
}
}
impl core::fmt::Display for ErrorKind {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Overrun => write!(f, "The peripheral receive buffer was overrun"),
Self::Parity => write!(f, "Parity check failed"),
Self::Noise => write!(f, "Serial line is too noisy to read valid data"),
Self::FrameFormat => write!(
f,
"Received data does not conform to the peripheral configuration"
),
Self::Other => write!(
f,
"A different error occurred. The original error may contain more information"
),
}
}
}
pub trait ErrorType {
type Error: Error;
}
impl<T: ErrorType + ?Sized> ErrorType for &mut T {
type Error = T::Error;
}
pub trait Read<Word: Copy = u8>: ErrorType {
fn read(&mut self) -> nb::Result<Word, Self::Error>;
}
impl<T: Read<Word> + ?Sized, Word: Copy> Read<Word> for &mut T {
#[inline]
fn read(&mut self) -> nb::Result<Word, Self::Error> {
T::read(self)
}
}
pub trait Write<Word: Copy = u8>: ErrorType {
fn write(&mut self, word: Word) -> nb::Result<(), Self::Error>;
fn flush(&mut self) -> nb::Result<(), Self::Error>;
}
impl<T: Write<Word> + ?Sized, Word: Copy> Write<Word> for &mut T {
#[inline]
fn write(&mut self, word: Word) -> nb::Result<(), Self::Error> {
T::write(self, word)
}
#[inline]
fn flush(&mut self) -> nb::Result<(), Self::Error> {
T::flush(self)
}
}
impl<Word, Error: self::Error> core::fmt::Write for dyn Write<Word, Error = Error> + '_
where
Word: Copy + From<u8>,
{
#[inline]
fn write_str(&mut self, s: &str) -> core::fmt::Result {
let _ = s
.bytes()
.map(|c| nb::block!(self.write(Word::from(c))))
.last();
Ok(())
}
}