|
2 | 2 |
|
3 | 3 | use core::{convert::From, ops::Not};
|
4 | 4 |
|
5 |
| -/// GPIO error type trait |
| 5 | +/// Error |
| 6 | +pub trait Error: core::fmt::Debug { |
| 7 | + /// Convert error to a generic error kind |
| 8 | + /// |
| 9 | + /// By using this method, errors freely defined by HAL implementations |
| 10 | + /// can be converted to a set of generic errors upon which generic |
| 11 | + /// code can act. |
| 12 | + fn kind(&self) -> ErrorKind; |
| 13 | +} |
| 14 | + |
| 15 | +impl Error for core::convert::Infallible { |
| 16 | + fn kind(&self) -> ErrorKind { |
| 17 | + match *self {} |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +/// Error kind |
| 22 | +/// |
| 23 | +/// This represents a common set of operation errors. HAL implementations are |
| 24 | +/// free to define more specific or additional error types. However, by providing |
| 25 | +/// a mapping to these common errors, generic code can still react to them. |
| 26 | +#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] |
| 27 | +#[non_exhaustive] |
| 28 | +pub enum ErrorKind { |
| 29 | + /// A different error occurred. The original error may contain more information. |
| 30 | + Other, |
| 31 | +} |
| 32 | + |
| 33 | +impl Error for ErrorKind { |
| 34 | + fn kind(&self) -> ErrorKind { |
| 35 | + *self |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl core::fmt::Display for ErrorKind { |
| 40 | + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 41 | + match self { |
| 42 | + Self::Other => write!( |
| 43 | + f, |
| 44 | + "A different error occurred. The original error may contain more information" |
| 45 | + ), |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/// Error type trait |
6 | 51 | ///
|
7 | 52 | /// This just defines the error type, to be used by the other traits.
|
8 | 53 | pub trait ErrorType {
|
9 | 54 | /// Error type
|
10 |
| - type Error: core::fmt::Debug; |
| 55 | + type Error: Error; |
11 | 56 | }
|
12 | 57 |
|
13 | 58 | impl<T: ErrorType> ErrorType for &T {
|
14 | 59 | type Error = T::Error;
|
15 | 60 | }
|
| 61 | + |
16 | 62 | impl<T: ErrorType> ErrorType for &mut T {
|
17 | 63 | type Error = T::Error;
|
18 | 64 | }
|
|
0 commit comments