diff --git a/CHANGELOG.md b/CHANGELOG.md index cd6a5b38..4bc7319f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - `timer.rs` refactoring +### Added + +- `embedded_io::Read` trait implemented for `serial` + ## [v0.11.0] - 2025-09-09 ### Breaking changes diff --git a/src/serial/hal_1.rs b/src/serial/hal_1.rs index b351971e..990e5fe8 100644 --- a/src/serial/hal_1.rs +++ b/src/serial/hal_1.rs @@ -101,7 +101,7 @@ mod nb { mod io { use super::super::{Error, Instance, Rx, Serial, Tx}; - use embedded_io::Write; + use embedded_io::{Read, Write}; impl embedded_io::Error for Error { // TODO: fix error conversion @@ -122,6 +122,19 @@ mod io { type Error = Error; } + impl Read for Rx { + fn read(&mut self, bytes: &mut [u8]) -> Result { + for (i, byte) in bytes.iter_mut().enumerate() { + match self.read() { + Ok(b) => *byte = b, + Err(nb::Error::WouldBlock) => return Ok(i), + Err(nb::Error::Other(e)) => return Err(e), + } + } + Ok(bytes.len()) + } + } + impl Write for Tx { fn write(&mut self, bytes: &[u8]) -> Result { let mut i = 0; @@ -147,6 +160,22 @@ mod io { } } + impl Read for Serial + where + Rx: Read, + { + fn read(&mut self, bytes: &mut [u8]) -> Result { + for (i, byte) in bytes.iter_mut().enumerate() { + match self.rx.read() { + Ok(b) => *byte = b, + Err(nb::Error::WouldBlock) => return Ok(i), + Err(nb::Error::Other(e)) => return Err(e), + } + } + Ok(bytes.len()) + } + } + impl Write for Serial where Tx: Write,