Skip to content

Commit

Permalink
io: add error kinds from std.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirbaio committed Jul 12, 2023
1 parent 64b66b5 commit e37cb22
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 3 deletions.
42 changes: 39 additions & 3 deletions embedded-io/src/adapters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,51 @@ impl<T: crate::Seek + ?Sized> std::io::Seek for ToStd<T> {
}
}

fn to_std_error<T: core::fmt::Debug>(err: T) -> std::io::Error {
let kind = std::io::ErrorKind::Other;
fn to_std_error<T: crate::Error>(err: T) -> std::io::Error {
let kind = match err.kind() {
crate::ErrorKind::NotFound => std::io::ErrorKind::NotFound,
crate::ErrorKind::PermissionDenied => std::io::ErrorKind::PermissionDenied,
crate::ErrorKind::ConnectionRefused => std::io::ErrorKind::ConnectionRefused,
crate::ErrorKind::ConnectionReset => std::io::ErrorKind::ConnectionReset,
crate::ErrorKind::ConnectionAborted => std::io::ErrorKind::ConnectionAborted,
crate::ErrorKind::NotConnected => std::io::ErrorKind::NotConnected,
crate::ErrorKind::AddrInUse => std::io::ErrorKind::AddrInUse,
crate::ErrorKind::AddrNotAvailable => std::io::ErrorKind::AddrNotAvailable,
crate::ErrorKind::BrokenPipe => std::io::ErrorKind::BrokenPipe,
crate::ErrorKind::AlreadyExists => std::io::ErrorKind::AlreadyExists,
crate::ErrorKind::InvalidInput => std::io::ErrorKind::InvalidInput,
crate::ErrorKind::InvalidData => std::io::ErrorKind::InvalidData,
crate::ErrorKind::TimedOut => std::io::ErrorKind::TimedOut,
crate::ErrorKind::Interrupted => std::io::ErrorKind::Interrupted,
crate::ErrorKind::Unsupported => std::io::ErrorKind::Unsupported,
crate::ErrorKind::OutOfMemory => std::io::ErrorKind::OutOfMemory,
_ => std::io::ErrorKind::Other,
};
std::io::Error::new(kind, format!("{:?}", err))
}

#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl crate::Error for std::io::Error {
fn kind(&self) -> crate::ErrorKind {
crate::ErrorKind::Other
match self.kind() {
std::io::ErrorKind::NotFound => crate::ErrorKind::NotFound,
std::io::ErrorKind::PermissionDenied => crate::ErrorKind::PermissionDenied,
std::io::ErrorKind::ConnectionRefused => crate::ErrorKind::ConnectionRefused,
std::io::ErrorKind::ConnectionReset => crate::ErrorKind::ConnectionReset,
std::io::ErrorKind::ConnectionAborted => crate::ErrorKind::ConnectionAborted,
std::io::ErrorKind::NotConnected => crate::ErrorKind::NotConnected,
std::io::ErrorKind::AddrInUse => crate::ErrorKind::AddrInUse,
std::io::ErrorKind::AddrNotAvailable => crate::ErrorKind::AddrNotAvailable,
std::io::ErrorKind::BrokenPipe => crate::ErrorKind::BrokenPipe,
std::io::ErrorKind::AlreadyExists => crate::ErrorKind::AlreadyExists,
std::io::ErrorKind::InvalidInput => crate::ErrorKind::InvalidInput,
std::io::ErrorKind::InvalidData => crate::ErrorKind::InvalidData,
std::io::ErrorKind::TimedOut => crate::ErrorKind::TimedOut,
std::io::ErrorKind::Interrupted => crate::ErrorKind::Interrupted,
std::io::ErrorKind::Unsupported => crate::ErrorKind::Unsupported,
std::io::ErrorKind::OutOfMemory => crate::ErrorKind::OutOfMemory,
_ => crate::ErrorKind::Other,
}
}
}

Expand Down
58 changes: 58 additions & 0 deletions embedded-io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,67 @@ pub enum SeekFrom {
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[non_exhaustive]
/// Possible kinds of errors.
///
/// This list is intended to grow over time and it is not recommended to
/// exhaustively match against it. In application code, use `match` for the `ErrorKind`
/// values you are expecting; use `_` to match "all other errors".
///
/// This is the `embedded-io` equivalent of [`std::io::ErrorKind`], except with the following changes:
///
/// - `WouldBlock` is removed, since `embedded-io` traits are always blocking. See the [crate-level documentation](crate) for details.
/// - `WriteZero` is removed, since it is a separate variant in [`WriteAllError`] and [`WriteFmtError`].
pub enum ErrorKind {
/// Unspecified error kind.
Other,

/// An entity was not found, often a file.
NotFound,
/// The operation lacked the necessary privileges to complete.
PermissionDenied,
/// The connection was refused by the remote server.
ConnectionRefused,
/// The connection was reset by the remote server.
ConnectionReset,
/// The connection was aborted (terminated) by the remote server.
ConnectionAborted,
/// The network operation failed because it was not connected yet.
NotConnected,
/// A socket address could not be bound because the address is already in
/// use elsewhere.
AddrInUse,
/// A nonexistent interface was requested or the requested address was not
/// local.
AddrNotAvailable,
/// The operation failed because a pipe was closed.
BrokenPipe,
/// An entity already exists, often a file.
AlreadyExists,
/// A parameter was incorrect.
InvalidInput,
/// Data not valid for the operation were encountered.
///
/// Unlike [`InvalidInput`], this typically means that the operation
/// parameters were valid, however the error was caused by malformed
/// input data.
///
/// For example, a function that reads a file into a string will error with
/// `InvalidData` if the file's contents are not valid UTF-8.
///
/// [`InvalidInput`]: ErrorKind::InvalidInput
InvalidData,
/// The I/O operation's timeout expired, causing it to be canceled.
TimedOut,
/// This operation was interrupted.
///
/// Interrupted operations can typically be retried.
Interrupted,
/// This operation is unsupported on this platform.
///
/// This means that the operation can never succeed.
Unsupported,
/// An operation could not be completed, because it failed
/// to allocate enough memory.
OutOfMemory,
}

/// Error trait.
Expand Down

0 comments on commit e37cb22

Please sign in to comment.