Skip to content

Commit 3a4814f

Browse files
committed
io: Add From impls to convert between ErrorKind and std::io::ErrorKind.
1 parent 3f4c8d9 commit 3a4814f

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

embedded-io/CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10-
- Added `ReadReady`, `WriteReady` traits. They allow peeking whether the I/O handle is ready to read/write, so they allow using the traits in a non-blocking way.
10+
- Add `ReadReady`, `WriteReady` traits. They allow peeking whether the I/O handle is ready to read/write, so they allow using the traits in a non-blocking way.
11+
- Add variants to `ErrorKind` mirroring `std::io::ErrorKind`.
12+
- Add `From` impls to convert between `ErrorKind` and `std::io::ErrorKind`.
1113
- Moved `embedded_io::blocking` to the crate root.
1214
- Split async traits to the `embedded-io-async` crate.
1315
- Split async trait adapters to separate crates.

embedded-io/src/lib.rs

+52
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,58 @@ pub enum ErrorKind {
9292
OutOfMemory,
9393
}
9494

95+
#[cfg(feature = "std")]
96+
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
97+
impl From<ErrorKind> for std::io::ErrorKind {
98+
fn from(value: ErrorKind) -> Self {
99+
match value {
100+
ErrorKind::NotFound => std::io::ErrorKind::NotFound,
101+
ErrorKind::PermissionDenied => std::io::ErrorKind::PermissionDenied,
102+
ErrorKind::ConnectionRefused => std::io::ErrorKind::ConnectionRefused,
103+
ErrorKind::ConnectionReset => std::io::ErrorKind::ConnectionReset,
104+
ErrorKind::ConnectionAborted => std::io::ErrorKind::ConnectionAborted,
105+
ErrorKind::NotConnected => std::io::ErrorKind::NotConnected,
106+
ErrorKind::AddrInUse => std::io::ErrorKind::AddrInUse,
107+
ErrorKind::AddrNotAvailable => std::io::ErrorKind::AddrNotAvailable,
108+
ErrorKind::BrokenPipe => std::io::ErrorKind::BrokenPipe,
109+
ErrorKind::AlreadyExists => std::io::ErrorKind::AlreadyExists,
110+
ErrorKind::InvalidInput => std::io::ErrorKind::InvalidInput,
111+
ErrorKind::InvalidData => std::io::ErrorKind::InvalidData,
112+
ErrorKind::TimedOut => std::io::ErrorKind::TimedOut,
113+
ErrorKind::Interrupted => std::io::ErrorKind::Interrupted,
114+
ErrorKind::Unsupported => std::io::ErrorKind::Unsupported,
115+
ErrorKind::OutOfMemory => std::io::ErrorKind::OutOfMemory,
116+
_ => std::io::ErrorKind::Other,
117+
}
118+
}
119+
}
120+
121+
#[cfg(feature = "std")]
122+
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
123+
impl From<std::io::ErrorKind> for ErrorKind {
124+
fn from(value: std::io::ErrorKind) -> Self {
125+
match value {
126+
std::io::ErrorKind::NotFound => ErrorKind::NotFound,
127+
std::io::ErrorKind::PermissionDenied => ErrorKind::PermissionDenied,
128+
std::io::ErrorKind::ConnectionRefused => ErrorKind::ConnectionRefused,
129+
std::io::ErrorKind::ConnectionReset => ErrorKind::ConnectionReset,
130+
std::io::ErrorKind::ConnectionAborted => ErrorKind::ConnectionAborted,
131+
std::io::ErrorKind::NotConnected => ErrorKind::NotConnected,
132+
std::io::ErrorKind::AddrInUse => ErrorKind::AddrInUse,
133+
std::io::ErrorKind::AddrNotAvailable => ErrorKind::AddrNotAvailable,
134+
std::io::ErrorKind::BrokenPipe => ErrorKind::BrokenPipe,
135+
std::io::ErrorKind::AlreadyExists => ErrorKind::AlreadyExists,
136+
std::io::ErrorKind::InvalidInput => ErrorKind::InvalidInput,
137+
std::io::ErrorKind::InvalidData => ErrorKind::InvalidData,
138+
std::io::ErrorKind::TimedOut => ErrorKind::TimedOut,
139+
std::io::ErrorKind::Interrupted => ErrorKind::Interrupted,
140+
std::io::ErrorKind::Unsupported => ErrorKind::Unsupported,
141+
std::io::ErrorKind::OutOfMemory => ErrorKind::OutOfMemory,
142+
_ => ErrorKind::Other,
143+
}
144+
}
145+
}
146+
95147
/// Error trait.
96148
///
97149
/// This trait allows generic code to do limited inspecting of errors,

0 commit comments

Comments
 (0)