Skip to content
Snippets Groups Projects
Commit e609ab02 authored by Stanislav Kusovskyi's avatar Stanislav Kusovskyi Committed by Patrick José Pereira
Browse files

feat: add read method to Read trait for embedded traits

parent 4cd498bd
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,10 @@ const _: () = panic!("Only one of 'embedded' and 'embedded-hal-02' features can
/// Replacement for std::io::Read + byteorder::ReadBytesExt in no_std envs
pub trait Read {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, MessageReadError> {
self.read_exact(buf).map(|_| buf.len())
}
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), MessageReadError>;
}
......
......@@ -37,6 +37,15 @@ pub enum MessageReadError {
Parse(ParserError),
}
impl MessageReadError {
pub fn eof() -> Self {
#[cfg(feature = "std")]
return Self::Io(std::io::ErrorKind::UnexpectedEof.into());
#[cfg(any(feature = "embedded", feature = "embedded-hal-02"))]
return Self::Io;
}
}
impl Display for MessageReadError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match self {
......
......@@ -135,9 +135,7 @@ impl<R: Read, const BUFFER_SIZE: usize> PeekReader<R, BUFFER_SIZE> {
let bytes_read = self.reader.read(&mut buf[..bytes_to_read])?;
if bytes_read == 0 {
return Err(MessageReadError::Io(
std::io::ErrorKind::UnexpectedEof.into(),
));
return Err(MessageReadError::eof());
}
// if some bytes were read, add them to the buffer
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment