diff --git a/examples/embedded_hal.rs b/examples/embedded_hal.rs index b424dd33..f463374a 100644 --- a/examples/embedded_hal.rs +++ b/examples/embedded_hal.rs @@ -2,13 +2,15 @@ //! instance can be passed to functions / drivers that expect a type that //! implements the embedded-hal traits. -use embedded_hal_nb::serial; +fn take_nonblocking_reader>(_r: &R) { + // do nothing, but things should typecheck +} -fn take_reader>(_r: &R) { +fn take_nonblocking_writer>(_w: &W) { // do nothing, but things should typecheck } -fn take_writer>(_w: &W) { +fn take_blocking_writer>(_w: &W) { // do nothing, but things should typecheck } @@ -16,6 +18,7 @@ fn main() { let port = serialport::new("/dev/null", 9600) .open() .expect("This example isn't meant for running. It just demonstrates compatibility with embedded-hal on a type level."); - take_reader(&port); - take_writer(&port); + take_nonblocking_reader(&port); + take_nonblocking_writer(&port); + take_blocking_writer(&port); } diff --git a/src/embedded.rs b/src/embedded.rs index 712b4d0a..bd263fd1 100644 --- a/src/embedded.rs +++ b/src/embedded.rs @@ -4,7 +4,7 @@ use std::io; -use embedded_hal_nb::serial; +use embedded_hal::serial::{ErrorType, ErrorKind}; use crate::SerialPort; @@ -13,47 +13,75 @@ pub struct SerialError { kind: io::ErrorKind, } -// Implement `serial::Error` for SerialError -impl serial::Error for SerialError { - fn kind(&self) -> serial::ErrorKind { +impl embedded_hal::serial::Error for SerialError { + fn kind(&self) -> ErrorKind { #[allow(clippy::match_single_binding)] match self.kind { - _other => serial::ErrorKind::Other, + _ => ErrorKind::Other, } } } -fn io_error_to_nb(err: io::Error) -> nb::Error { - match err.kind() { - io::ErrorKind::WouldBlock | io::ErrorKind::Interrupted => nb::Error::WouldBlock, - other => nb::Error::Other(SerialError { kind: other }), +impl From for SerialError { + fn from(e: io::Error) -> Self { + SerialError { + kind: e.kind(), + } } } -impl serial::ErrorType for Box { +impl ErrorType for Box { type Error = SerialError; } -impl serial::Read for Box { - fn read(&mut self) -> nb::Result { - let mut buffer = [0; 1]; - let bytes_read = io::Read::read(self, &mut buffer).map_err(io_error_to_nb)?; - if bytes_read > 0 { - Ok(buffer[0]) - } else { - Err(nb::Error::WouldBlock) + +mod nonblocking { + use super::*; + use embedded_hal_nb::serial; + + fn io_error_to_nb(err: io::Error) -> nb::Error { + match err.kind() { + io::ErrorKind::WouldBlock | io::ErrorKind::Interrupted => nb::Error::WouldBlock, + other => nb::Error::Other(SerialError { kind: other }), } } -} -impl serial::Write for Box { - fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> { - io::Write::write(self, &[word]) - .map_err(io_error_to_nb) - .map(|_| ()) + impl serial::Read for Box { + fn read(&mut self) -> nb::Result { + let mut buffer = [0; 1]; + let bytes_read = io::Read::read(self, &mut buffer).map_err(io_error_to_nb)?; + if bytes_read > 0 { + Ok(buffer[0]) + } else { + Err(nb::Error::WouldBlock) + } + } } - fn flush(&mut self) -> nb::Result<(), Self::Error> { - io::Write::flush(self).map_err(io_error_to_nb) + impl serial::Write for Box { + fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> { + io::Write::write(self, &[word]) + .map_err(io_error_to_nb) + .map(|_| ()) + } + + fn flush(&mut self) -> nb::Result<(), Self::Error> { + io::Write::flush(self).map_err(io_error_to_nb) + } + } +} + +mod blocking { + use super::*; + use embedded_hal::serial; + + impl serial::Write for Box { + fn write(&mut self, buffer: &[u8]) -> Result<(), Self::Error> { + Ok(io::Write::write_all(self, buffer)?) + } + + fn flush(&mut self) -> Result<(), Self::Error> { + Ok(io::Write::flush(self)?) + } } }