Skip to content

Commit 10f2082

Browse files
committed
io: Fix Read::read_exact std adapter
1 parent e12dbf6 commit 10f2082

File tree

1 file changed

+22
-1
lines changed
  • embedded-io-adapters/src

1 file changed

+22
-1
lines changed

embedded-io-adapters/src/std.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Adapters to/from `std::io` traits.
22
3-
use embedded_io::Error as _;
3+
use embedded_io::{Error as _, ReadExactError};
44

55
/// Adapter from `std::io` traits.
66
#[derive(Clone)]
@@ -40,6 +40,16 @@ impl<T: std::io::Read + ?Sized> embedded_io::Read for FromStd<T> {
4040
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
4141
self.inner.read(buf)
4242
}
43+
44+
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), ReadExactError<Self::Error>> {
45+
match self.inner.read_exact(buf) {
46+
Ok(()) => Ok(()),
47+
Err(error) if error.kind() == std::io::ErrorKind::UnexpectedEof => {
48+
Err(embedded_io::ReadExactError::UnexpectedEof)
49+
}
50+
Err(error) => Err(error.into()),
51+
}
52+
}
4353
}
4454

4555
impl<T: std::io::BufRead + ?Sized> embedded_io::BufRead for FromStd<T> {
@@ -105,6 +115,17 @@ impl<T: embedded_io::Read + ?Sized> std::io::Read for ToStd<T> {
105115
fn read(&mut self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
106116
self.inner.read(buf).map_err(to_std_error)
107117
}
118+
119+
fn read_exact(&mut self, buf: &mut [u8]) -> std::io::Result<()> {
120+
match self.inner.read_exact(buf) {
121+
Ok(()) => Ok(()),
122+
Err(e @ ReadExactError::UnexpectedEof) => Err(std::io::Error::new(
123+
std::io::ErrorKind::UnexpectedEof,
124+
format!("{e:?}"),
125+
)),
126+
Err(ReadExactError::Other(e)) => Err(to_std_error(e)),
127+
}
128+
}
108129
}
109130

110131
impl<T: embedded_io::Write + ?Sized> std::io::Write for ToStd<T> {

0 commit comments

Comments
 (0)