Skip to content

Commit fd19c0b

Browse files
committed
Missing ReadExact & WriteAll error impls for std
- Moves `to_std_error` to embedded-io behind the std feature - Add `ReadExactError` for `std::io::Error` - Add `WriteAllError` for `std::io::Error`
1 parent 2bbd867 commit fd19c0b

File tree

3 files changed

+52
-23
lines changed

3 files changed

+52
-23
lines changed

embedded-io-adapters/src/std.rs

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Adapters to/from `std::io` traits.
22
3+
use embedded_io::to_std_error;
4+
35
/// Adapter from `std::io` traits.
46
#[derive(Clone)]
57
pub struct FromStd<T: ?Sized> {
@@ -114,26 +116,3 @@ impl<T: embedded_io::Seek + ?Sized> std::io::Seek for ToStd<T> {
114116
self.inner.seek(pos.into()).map_err(to_std_error)
115117
}
116118
}
117-
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))
139-
}

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+
- Move `to_std_error` to embedded-io and 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: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,61 @@ impl<E> From<E> for ReadExactError<E> {
227227
}
228228
}
229229

230+
#[cfg(feature = "std")]
231+
impl From<ReadExactError<std::io::Error>> for std::io::Error {
232+
fn from(err: ReadExactError<std::io::Error>) -> Self {
233+
match err {
234+
ReadExactError::UnexpectedEof => {
235+
std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "UnexpectedEof".to_owned())
236+
}
237+
ReadExactError::Other(e) => to_std_error(e),
238+
}
239+
}
240+
}
241+
242+
#[cfg(feature = "std")]
243+
impl From<WriteAllError<std::io::Error>> for std::io::Error {
244+
fn from(err: WriteAllError<std::io::Error>) -> Self {
245+
match err {
246+
WriteAllError::WriteZero => {
247+
std::io::Error::new(std::io::ErrorKind::WriteZero, "WriteZero".to_owned())
248+
}
249+
WriteAllError::Other(e) => to_std_error(e),
250+
}
251+
}
252+
}
253+
230254
impl<E: fmt::Debug> fmt::Display for ReadExactError<E> {
231255
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
232256
write!(f, "{:?}", self)
233257
}
234258
}
235259

260+
#[cfg(feature = "std")]
261+
/// Convert a embedded-io error to a std::io::Error
262+
pub fn to_std_error<T: crate::Error>(err: T) -> std::io::Error {
263+
let kind = match err.kind() {
264+
crate::ErrorKind::NotFound => std::io::ErrorKind::NotFound,
265+
crate::ErrorKind::PermissionDenied => std::io::ErrorKind::PermissionDenied,
266+
crate::ErrorKind::ConnectionRefused => std::io::ErrorKind::ConnectionRefused,
267+
crate::ErrorKind::ConnectionReset => std::io::ErrorKind::ConnectionReset,
268+
crate::ErrorKind::ConnectionAborted => std::io::ErrorKind::ConnectionAborted,
269+
crate::ErrorKind::NotConnected => std::io::ErrorKind::NotConnected,
270+
crate::ErrorKind::AddrInUse => std::io::ErrorKind::AddrInUse,
271+
crate::ErrorKind::AddrNotAvailable => std::io::ErrorKind::AddrNotAvailable,
272+
crate::ErrorKind::BrokenPipe => std::io::ErrorKind::BrokenPipe,
273+
crate::ErrorKind::AlreadyExists => std::io::ErrorKind::AlreadyExists,
274+
crate::ErrorKind::InvalidInput => std::io::ErrorKind::InvalidInput,
275+
crate::ErrorKind::InvalidData => std::io::ErrorKind::InvalidData,
276+
crate::ErrorKind::TimedOut => std::io::ErrorKind::TimedOut,
277+
crate::ErrorKind::Interrupted => std::io::ErrorKind::Interrupted,
278+
crate::ErrorKind::Unsupported => std::io::ErrorKind::Unsupported,
279+
crate::ErrorKind::OutOfMemory => std::io::ErrorKind::OutOfMemory,
280+
_ => std::io::ErrorKind::Other,
281+
};
282+
std::io::Error::new(kind, format!("{:?}", err))
283+
}
284+
236285
#[cfg(feature = "std")]
237286
impl<E: fmt::Debug> std::error::Error for ReadExactError<E> {}
238287

0 commit comments

Comments
 (0)