Skip to content

Commit 6115eda

Browse files
committed
io: add error kinds from std.
1 parent 7aaa585 commit 6115eda

File tree

2 files changed

+97
-3
lines changed

2 files changed

+97
-3
lines changed

embedded-io/src/adapters.rs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,51 @@ impl<T: crate::Seek + ?Sized> std::io::Seek for ToStd<T> {
126126
}
127127
}
128128

129-
fn to_std_error<T: core::fmt::Debug>(err: T) -> std::io::Error {
130-
let kind = std::io::ErrorKind::Other;
129+
fn to_std_error<T: crate::Error>(err: T) -> std::io::Error {
130+
let kind = match err.kind() {
131+
crate::ErrorKind::NotFound => std::io::ErrorKind::NotFound,
132+
crate::ErrorKind::PermissionDenied => std::io::ErrorKind::PermissionDenied,
133+
crate::ErrorKind::ConnectionRefused => std::io::ErrorKind::ConnectionRefused,
134+
crate::ErrorKind::ConnectionReset => std::io::ErrorKind::ConnectionReset,
135+
crate::ErrorKind::ConnectionAborted => std::io::ErrorKind::ConnectionAborted,
136+
crate::ErrorKind::NotConnected => std::io::ErrorKind::NotConnected,
137+
crate::ErrorKind::AddrInUse => std::io::ErrorKind::AddrInUse,
138+
crate::ErrorKind::AddrNotAvailable => std::io::ErrorKind::AddrNotAvailable,
139+
crate::ErrorKind::BrokenPipe => std::io::ErrorKind::BrokenPipe,
140+
crate::ErrorKind::AlreadyExists => std::io::ErrorKind::AlreadyExists,
141+
crate::ErrorKind::InvalidInput => std::io::ErrorKind::InvalidInput,
142+
crate::ErrorKind::InvalidData => std::io::ErrorKind::InvalidData,
143+
crate::ErrorKind::TimedOut => std::io::ErrorKind::TimedOut,
144+
crate::ErrorKind::Interrupted => std::io::ErrorKind::Interrupted,
145+
crate::ErrorKind::Unsupported => std::io::ErrorKind::Unsupported,
146+
crate::ErrorKind::OutOfMemory => std::io::ErrorKind::OutOfMemory,
147+
_ => std::io::ErrorKind::Other,
148+
};
131149
std::io::Error::new(kind, format!("{:?}", err))
132150
}
133151

134152
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
135153
impl crate::Error for std::io::Error {
136154
fn kind(&self) -> crate::ErrorKind {
137-
crate::ErrorKind::Other
155+
match self.kind() {
156+
std::io::ErrorKind::NotFound => crate::ErrorKind::NotFound,
157+
std::io::ErrorKind::PermissionDenied => crate::ErrorKind::PermissionDenied,
158+
std::io::ErrorKind::ConnectionRefused => crate::ErrorKind::ConnectionRefused,
159+
std::io::ErrorKind::ConnectionReset => crate::ErrorKind::ConnectionReset,
160+
std::io::ErrorKind::ConnectionAborted => crate::ErrorKind::ConnectionAborted,
161+
std::io::ErrorKind::NotConnected => crate::ErrorKind::NotConnected,
162+
std::io::ErrorKind::AddrInUse => crate::ErrorKind::AddrInUse,
163+
std::io::ErrorKind::AddrNotAvailable => crate::ErrorKind::AddrNotAvailable,
164+
std::io::ErrorKind::BrokenPipe => crate::ErrorKind::BrokenPipe,
165+
std::io::ErrorKind::AlreadyExists => crate::ErrorKind::AlreadyExists,
166+
std::io::ErrorKind::InvalidInput => crate::ErrorKind::InvalidInput,
167+
std::io::ErrorKind::InvalidData => crate::ErrorKind::InvalidData,
168+
std::io::ErrorKind::TimedOut => crate::ErrorKind::TimedOut,
169+
std::io::ErrorKind::Interrupted => crate::ErrorKind::Interrupted,
170+
std::io::ErrorKind::Unsupported => crate::ErrorKind::Unsupported,
171+
std::io::ErrorKind::OutOfMemory => crate::ErrorKind::OutOfMemory,
172+
_ => crate::ErrorKind::Other,
173+
}
138174
}
139175
}
140176

embedded-io/src/lib.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,67 @@ pub enum SeekFrom {
2929
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
3030
#[non_exhaustive]
3131
/// Possible kinds of errors.
32+
///
33+
/// This list is intended to grow over time and it is not recommended to
34+
/// exhaustively match against it. In application code, use `match` for the `ErrorKind`
35+
/// values you are expecting; use `_` to match "all other errors".
36+
///
37+
/// This is the `embedded-io` equivalent of [`std::io::ErrorKind`], except with the following changes:
38+
///
39+
/// - `WouldBlock` is removed, since `embedded-io` traits are always blocking. See the [crate-level documentation](crate) for details.
40+
/// - `WriteZero` is removed, since it is a separate variant in [`WriteAllError`] and [`WriteFmtError`].
3241
pub enum ErrorKind {
3342
/// Unspecified error kind.
3443
Other,
44+
45+
/// An entity was not found, often a file.
46+
NotFound,
47+
/// The operation lacked the necessary privileges to complete.
48+
PermissionDenied,
49+
/// The connection was refused by the remote server.
50+
ConnectionRefused,
51+
/// The connection was reset by the remote server.
52+
ConnectionReset,
53+
/// The connection was aborted (terminated) by the remote server.
54+
ConnectionAborted,
55+
/// The network operation failed because it was not connected yet.
56+
NotConnected,
57+
/// A socket address could not be bound because the address is already in
58+
/// use elsewhere.
59+
AddrInUse,
60+
/// A nonexistent interface was requested or the requested address was not
61+
/// local.
62+
AddrNotAvailable,
63+
/// The operation failed because a pipe was closed.
64+
BrokenPipe,
65+
/// An entity already exists, often a file.
66+
AlreadyExists,
67+
/// A parameter was incorrect.
68+
InvalidInput,
69+
/// Data not valid for the operation were encountered.
70+
///
71+
/// Unlike [`InvalidInput`], this typically means that the operation
72+
/// parameters were valid, however the error was caused by malformed
73+
/// input data.
74+
///
75+
/// For example, a function that reads a file into a string will error with
76+
/// `InvalidData` if the file's contents are not valid UTF-8.
77+
///
78+
/// [`InvalidInput`]: ErrorKind::InvalidInput
79+
InvalidData,
80+
/// The I/O operation's timeout expired, causing it to be canceled.
81+
TimedOut,
82+
/// This operation was interrupted.
83+
///
84+
/// Interrupted operations can typically be retried.
85+
Interrupted,
86+
/// This operation is unsupported on this platform.
87+
///
88+
/// This means that the operation can never succeed.
89+
Unsupported,
90+
/// An operation could not be completed, because it failed
91+
/// to allocate enough memory.
92+
OutOfMemory,
3593
}
3694

3795
/// Error trait.

0 commit comments

Comments
 (0)