Merge pull request #700 from Conaclos/std-io-adaptaer-read-exact-fix

io-adapter: Forward `Read::read_exact` in the std adapters
This commit is contained in:
Dario Nieuwenhuis 2025-09-09 13:16:32 +00:00 committed by GitHub
commit e833acdfd8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -40,6 +40,19 @@ impl<T: std::io::Read + ?Sized> embedded_io::Read for FromStd<T> {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
self.inner.read(buf)
}
fn read_exact(
&mut self,
buf: &mut [u8],
) -> Result<(), embedded_io::ReadExactError<Self::Error>> {
match self.inner.read_exact(buf) {
Ok(()) => Ok(()),
Err(error) if error.kind() == std::io::ErrorKind::UnexpectedEof => {
Err(embedded_io::ReadExactError::UnexpectedEof)
}
Err(error) => Err(error.into()),
}
}
}
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> {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
self.inner.read(buf).map_err(to_std_error)
}
fn read_exact(&mut self, buf: &mut [u8]) -> std::io::Result<()> {
match self.inner.read_exact(buf) {
Ok(()) => Ok(()),
Err(e @ embedded_io::ReadExactError::UnexpectedEof) => Err(std::io::Error::new(
std::io::ErrorKind::UnexpectedEof,
format!("{e:?}"),
)),
Err(embedded_io::ReadExactError::Other(e)) => Err(to_std_error(e)),
}
}
}
impl<T: embedded_io::Write + ?Sized> std::io::Write for ToStd<T> {