From e481084a24572016d383271cba7c16f7daac3b15 Mon Sep 17 00:00:00 2001 From: Stanislav Kusovskyi Date: Tue, 24 Dec 2024 13:17:51 +0000 Subject: [PATCH] feat: add read method to Read trait for embedded traits --- mavlink-core/src/embedded.rs | 4 ++++ mavlink-core/src/error.rs | 9 +++++++++ mavlink-core/src/peek_reader.rs | 4 +--- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/mavlink-core/src/embedded.rs b/mavlink-core/src/embedded.rs index 96917e506c..3d1e7e5e14 100644 --- a/mavlink-core/src/embedded.rs +++ b/mavlink-core/src/embedded.rs @@ -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 { + self.read_exact(buf).map(|_| buf.len()) + } + fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), MessageReadError>; } diff --git a/mavlink-core/src/error.rs b/mavlink-core/src/error.rs index 49394f8d64..08e0bed744 100644 --- a/mavlink-core/src/error.rs +++ b/mavlink-core/src/error.rs @@ -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 { diff --git a/mavlink-core/src/peek_reader.rs b/mavlink-core/src/peek_reader.rs index b67da6a32b..ec3c1de20b 100644 --- a/mavlink-core/src/peek_reader.rs +++ b/mavlink-core/src/peek_reader.rs @@ -135,9 +135,7 @@ impl PeekReader { 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