Skip to content

Commit e833acd

Browse files
authored
Merge pull request #700 from Conaclos/std-io-adaptaer-read-exact-fix
io-adapter: Forward `Read::read_exact` in the std adapters
2 parents ef9b7a7 + b4c35b6 commit e833acd

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> {
@@ -125,6 +138,17 @@ impl<T: embedded_io::Read + ?Sized> std::io::Read for ToStd<T> {
125138
fn read(&mut self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
126139
self.inner.read(buf).map_err(to_std_error)
127140
}
141+
142+
fn read_exact(&mut self, buf: &mut [u8]) -> std::io::Result<()> {
143+
match self.inner.read_exact(buf) {
144+
Ok(()) => Ok(()),
145+
Err(e @ embedded_io::ReadExactError::UnexpectedEof) => Err(std::io::Error::new(
146+
std::io::ErrorKind::UnexpectedEof,
147+
format!("{e:?}"),
148+
)),
149+
Err(embedded_io::ReadExactError::Other(e)) => Err(to_std_error(e)),
150+
}
151+
}
128152
}
129153

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

0 commit comments

Comments
 (0)