Skip to content

Commit 332d0ce

Browse files
MabezDevDirbaio
authored andcommitted
Missing ReadExact & WriteAll error impls for std
- Add `ReadExactError` for `std::io::Error` - Add `WriteAllError` for `std::io::Error`
1 parent 981caa6 commit 332d0ce

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

embedded-io-adapters/src/std.rs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -115,25 +115,7 @@ impl<T: embedded_io::Seek + ?Sized> std::io::Seek for ToStd<T> {
115115
}
116116
}
117117

118-
fn to_std_error<T: embedded_io::Error>(err: T) -> std::io::Error {
119-
let kind = match err.kind() {
120-
embedded_io::ErrorKind::NotFound => std::io::ErrorKind::NotFound,
121-
embedded_io::ErrorKind::PermissionDenied => std::io::ErrorKind::PermissionDenied,
122-
embedded_io::ErrorKind::ConnectionRefused => std::io::ErrorKind::ConnectionRefused,
123-
embedded_io::ErrorKind::ConnectionReset => std::io::ErrorKind::ConnectionReset,
124-
embedded_io::ErrorKind::ConnectionAborted => std::io::ErrorKind::ConnectionAborted,
125-
embedded_io::ErrorKind::NotConnected => std::io::ErrorKind::NotConnected,
126-
embedded_io::ErrorKind::AddrInUse => std::io::ErrorKind::AddrInUse,
127-
embedded_io::ErrorKind::AddrNotAvailable => std::io::ErrorKind::AddrNotAvailable,
128-
embedded_io::ErrorKind::BrokenPipe => std::io::ErrorKind::BrokenPipe,
129-
embedded_io::ErrorKind::AlreadyExists => std::io::ErrorKind::AlreadyExists,
130-
embedded_io::ErrorKind::InvalidInput => std::io::ErrorKind::InvalidInput,
131-
embedded_io::ErrorKind::InvalidData => std::io::ErrorKind::InvalidData,
132-
embedded_io::ErrorKind::TimedOut => std::io::ErrorKind::TimedOut,
133-
embedded_io::ErrorKind::Interrupted => std::io::ErrorKind::Interrupted,
134-
embedded_io::ErrorKind::Unsupported => std::io::ErrorKind::Unsupported,
135-
embedded_io::ErrorKind::OutOfMemory => std::io::ErrorKind::OutOfMemory,
136-
_ => std::io::ErrorKind::Other,
137-
};
138-
std::io::Error::new(kind, format!("{:?}", err))
118+
/// Convert a embedded-io error to a std::io::Error
119+
pub fn to_std_error<T: embedded_io::Error>(err: T) -> std::io::Error {
120+
std::io::Error::new(err.kind().into(), format!("{:?}", err))
139121
}

embedded-io/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Moved `embedded_io::blocking` to the crate root.
1414
- Split async traits to the `embedded-io-async` crate.
1515
- Split trait adapters to the `embedded-io-adapters` crate.
16+
- Add `std::io` impls for `ReadExactError` & `WriteAllError`.
1617
- Rename trait `Io` to `ErrorKind`, for consistency with `embedded-hal`.
1718

1819
## 0.4.0 - 2022-11-25

embedded-io/src/lib.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,41 @@ impl<E> From<E> for ReadExactError<E> {
227227
}
228228
}
229229

230+
#[cfg(feature = "std")]
231+
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
232+
impl From<ReadExactError<std::io::Error>> for std::io::Error {
233+
fn from(err: ReadExactError<std::io::Error>) -> Self {
234+
match err {
235+
ReadExactError::UnexpectedEof => std::io::Error::new(
236+
std::io::ErrorKind::UnexpectedEof,
237+
"UnexpectedEof".to_owned(),
238+
),
239+
ReadExactError::Other(e) => std::io::Error::new(e.kind(), format!("{:?}", e)),
240+
}
241+
}
242+
}
243+
244+
#[cfg(feature = "std")]
245+
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
246+
impl From<WriteAllError<std::io::Error>> for std::io::Error {
247+
fn from(err: WriteAllError<std::io::Error>) -> Self {
248+
match err {
249+
WriteAllError::WriteZero => {
250+
std::io::Error::new(std::io::ErrorKind::WriteZero, "WriteZero".to_owned())
251+
}
252+
WriteAllError::Other(e) => std::io::Error::new(e.kind(), format!("{:?}", e)),
253+
}
254+
}
255+
}
256+
230257
impl<E: fmt::Debug> fmt::Display for ReadExactError<E> {
231258
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
232259
write!(f, "{:?}", self)
233260
}
234261
}
235262

236263
#[cfg(feature = "std")]
264+
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
237265
impl<E: fmt::Debug> std::error::Error for ReadExactError<E> {}
238266

239267
/// Error returned by [`Write::write_fmt`]
@@ -260,6 +288,7 @@ impl<E: fmt::Debug> fmt::Display for WriteFmtError<E> {
260288
}
261289

262290
#[cfg(feature = "std")]
291+
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
263292
impl<E: fmt::Debug> std::error::Error for WriteFmtError<E> {}
264293

265294
/// Error returned by [`Write::write_all`]
@@ -284,6 +313,7 @@ impl<E: fmt::Debug> fmt::Display for WriteAllError<E> {
284313
}
285314

286315
#[cfg(feature = "std")]
316+
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
287317
impl<E: fmt::Debug> std::error::Error for WriteAllError<E> {}
288318

289319
/// Blocking reader.

0 commit comments

Comments
 (0)