Skip to content

Commit f3e82dd

Browse files
committed
io: Fix FromStd::read_exact by mapping std::io::ErrorKind::UnexpectedEof to ReadExactError::UnexpectedEof
1 parent 1ffedc7 commit f3e82dd

File tree

1 file changed

+23
-2
lines changed
  • embedded-io-adapters/src

1 file changed

+23
-2
lines changed

embedded-io-adapters/src/std.rs

Lines changed: 23 additions & 2 deletions
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> {
@@ -101,10 +111,21 @@ impl<T: ?Sized> ToStd<T> {
101111
}
102112
}
103113

104-
impl<T: embedded_io::Read + ?Sized> std::io::Read for ToStd<T> {
114+
impl<T: embedded_io::Read + ?Sized> std::io::Read for ToStd<T>
115+
where
116+
T::Error: Into<std::io::Error>,
117+
{
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(ReadExactError::UnexpectedEof) => Err(std::io::ErrorKind::UnexpectedEof.into()),
126+
Err(ReadExactError::Other(other)) => Err(other.into()),
127+
}
128+
}
108129
}
109130

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

0 commit comments

Comments
 (0)