Skip to content

Commit b4c35b6

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

File tree

1 file changed

+24
-0
lines changed
  • embedded-io-adapters/src

1 file changed

+24
-0
lines changed

embedded-io-adapters/src/std.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ 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(
45+
&mut self,
46+
buf: &mut [u8],
47+
) -> Result<(), embedded_io::ReadExactError<Self::Error>> {
48+
match self.inner.read_exact(buf) {
49+
Ok(()) => Ok(()),
50+
Err(error) if error.kind() == std::io::ErrorKind::UnexpectedEof => {
51+
Err(embedded_io::ReadExactError::UnexpectedEof)
52+
}
53+
Err(error) => Err(error.into()),
54+
}
55+
}
4356
}
4457

4558
impl<T: std::io::BufRead + ?Sized> embedded_io::BufRead for FromStd<T> {
@@ -105,6 +118,17 @@ impl<T: embedded_io::Read + ?Sized> std::io::Read for ToStd<T> {
105118
fn read(&mut self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
106119
self.inner.read(buf).map_err(to_std_error)
107120
}
121+
122+
fn read_exact(&mut self, buf: &mut [u8]) -> std::io::Result<()> {
123+
match self.inner.read_exact(buf) {
124+
Ok(()) => Ok(()),
125+
Err(e @ embedded_io::ReadExactError::UnexpectedEof) => Err(std::io::Error::new(
126+
std::io::ErrorKind::UnexpectedEof,
127+
format!("{e:?}"),
128+
)),
129+
Err(embedded_io::ReadExactError::Other(e)) => Err(to_std_error(e)),
130+
}
131+
}
108132
}
109133

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

0 commit comments

Comments
 (0)