diff options
| author | Dario Nieuwenhuis <[email protected]> | 2020-10-31 16:37:09 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2020-10-31 16:37:09 +0100 |
| commit | b4a8b03c847f6197ecd4e63b531de365711bbfe6 (patch) | |
| tree | 234ed35988cd131a8cfb03ac7d6afb883069d5c6 | |
| parent | 57abd7064dcf04029e814f3e769b0ad0b2111118 (diff) | |
Improve std error compat
| -rw-r--r-- | embassy/src/io/error.rs | 81 |
1 files changed, 55 insertions, 26 deletions
diff --git a/embassy/src/io/error.rs b/embassy/src/io/error.rs index ee3263d55..8fa1d93ff 100644 --- a/embassy/src/io/error.rs +++ b/embassy/src/io/error.rs | |||
| @@ -1,8 +1,3 @@ | |||
| 1 | #[cfg(feature = "std")] | ||
| 2 | use core::convert::From; | ||
| 3 | #[cfg(feature = "std")] | ||
| 4 | use futures::io; | ||
| 5 | |||
| 6 | /// Categories of errors that can occur. | 1 | /// Categories of errors that can occur. |
| 7 | /// | 2 | /// |
| 8 | /// This list is intended to grow over time and it is not recommended to | 3 | /// This list is intended to grow over time and it is not recommended to |
| @@ -85,33 +80,67 @@ pub enum Error { | |||
| 85 | pub type Result<T> = core::result::Result<T, Error>; | 80 | pub type Result<T> = core::result::Result<T, Error>; |
| 86 | 81 | ||
| 87 | #[cfg(feature = "std")] | 82 | #[cfg(feature = "std")] |
| 88 | impl From<io::Error> for Error { | 83 | impl From<std::io::Error> for Error { |
| 89 | fn from(err: io::Error) -> Error { | 84 | fn from(err: std::io::Error) -> Error { |
| 90 | match err.kind() { | 85 | match err.kind() { |
| 91 | io::ErrorKind::NotFound => Error::NotFound, | 86 | std::io::ErrorKind::NotFound => Error::NotFound, |
| 92 | io::ErrorKind::PermissionDenied => Error::PermissionDenied, | 87 | std::io::ErrorKind::PermissionDenied => Error::PermissionDenied, |
| 93 | io::ErrorKind::ConnectionRefused => Error::ConnectionRefused, | 88 | std::io::ErrorKind::ConnectionRefused => Error::ConnectionRefused, |
| 94 | io::ErrorKind::ConnectionReset => Error::ConnectionReset, | 89 | std::io::ErrorKind::ConnectionReset => Error::ConnectionReset, |
| 95 | io::ErrorKind::ConnectionAborted => Error::ConnectionAborted, | 90 | std::io::ErrorKind::ConnectionAborted => Error::ConnectionAborted, |
| 96 | io::ErrorKind::NotConnected => Error::NotConnected, | 91 | std::io::ErrorKind::NotConnected => Error::NotConnected, |
| 97 | io::ErrorKind::AddrInUse => Error::AddrInUse, | 92 | std::io::ErrorKind::AddrInUse => Error::AddrInUse, |
| 98 | io::ErrorKind::AddrNotAvailable => Error::AddrNotAvailable, | 93 | std::io::ErrorKind::AddrNotAvailable => Error::AddrNotAvailable, |
| 99 | io::ErrorKind::BrokenPipe => Error::BrokenPipe, | 94 | std::io::ErrorKind::BrokenPipe => Error::BrokenPipe, |
| 100 | io::ErrorKind::AlreadyExists => Error::AlreadyExists, | 95 | std::io::ErrorKind::AlreadyExists => Error::AlreadyExists, |
| 101 | io::ErrorKind::WouldBlock => Error::WouldBlock, | 96 | std::io::ErrorKind::WouldBlock => Error::WouldBlock, |
| 102 | io::ErrorKind::InvalidInput => Error::InvalidInput, | 97 | std::io::ErrorKind::InvalidInput => Error::InvalidInput, |
| 103 | io::ErrorKind::InvalidData => Error::InvalidData, | 98 | std::io::ErrorKind::InvalidData => Error::InvalidData, |
| 104 | io::ErrorKind::TimedOut => Error::TimedOut, | 99 | std::io::ErrorKind::TimedOut => Error::TimedOut, |
| 105 | io::ErrorKind::WriteZero => Error::WriteZero, | 100 | std::io::ErrorKind::WriteZero => Error::WriteZero, |
| 106 | io::ErrorKind::Interrupted => Error::Interrupted, | 101 | std::io::ErrorKind::Interrupted => Error::Interrupted, |
| 107 | io::ErrorKind::UnexpectedEof => Error::UnexpectedEof, | 102 | std::io::ErrorKind::UnexpectedEof => Error::UnexpectedEof, |
| 108 | _ => Error::Other, | 103 | _ => Error::Other, |
| 109 | } | 104 | } |
| 110 | } | 105 | } |
| 111 | } | 106 | } |
| 112 | 107 | ||
| 113 | //#[cfg(feature = "std")] | 108 | #[cfg(feature = "std")] |
| 114 | //impl std::error::Error for Error {} | 109 | impl From<Error> for std::io::Error { |
| 110 | fn from(e: Error) -> Self { | ||
| 111 | let kind = match e { | ||
| 112 | Error::NotFound => std::io::ErrorKind::NotFound, | ||
| 113 | Error::PermissionDenied => std::io::ErrorKind::PermissionDenied, | ||
| 114 | Error::ConnectionRefused => std::io::ErrorKind::ConnectionRefused, | ||
| 115 | Error::ConnectionReset => std::io::ErrorKind::ConnectionReset, | ||
| 116 | Error::ConnectionAborted => std::io::ErrorKind::ConnectionAborted, | ||
| 117 | Error::NotConnected => std::io::ErrorKind::NotConnected, | ||
| 118 | Error::AddrInUse => std::io::ErrorKind::AddrInUse, | ||
| 119 | Error::AddrNotAvailable => std::io::ErrorKind::AddrNotAvailable, | ||
| 120 | Error::BrokenPipe => std::io::ErrorKind::BrokenPipe, | ||
| 121 | Error::AlreadyExists => std::io::ErrorKind::AlreadyExists, | ||
| 122 | Error::WouldBlock => std::io::ErrorKind::WouldBlock, | ||
| 123 | Error::InvalidInput => std::io::ErrorKind::InvalidInput, | ||
| 124 | Error::InvalidData => std::io::ErrorKind::InvalidData, | ||
| 125 | Error::TimedOut => std::io::ErrorKind::TimedOut, | ||
| 126 | Error::WriteZero => std::io::ErrorKind::WriteZero, | ||
| 127 | Error::Interrupted => std::io::ErrorKind::Interrupted, | ||
| 128 | Error::UnexpectedEof => std::io::ErrorKind::UnexpectedEof, | ||
| 129 | Error::Truncated => std::io::ErrorKind::Other, | ||
| 130 | Error::Other => std::io::ErrorKind::Other, | ||
| 131 | }; | ||
| 132 | std::io::Error::new(kind, "embassy::io::Error") | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | impl core::fmt::Display for Error { | ||
| 137 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||
| 138 | write!(f, "{:?}", self) | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | #[cfg(feature = "std")] | ||
| 143 | impl std::error::Error for Error {} | ||
| 115 | 144 | ||
| 116 | /* | 145 | /* |
| 117 | impl From<smoltcp::Error> for Error { | 146 | impl From<smoltcp::Error> for Error { |
