Skip to content

Commit ef20420

Browse files
bors[bot]Dirbaio
andauthored
Merge #431
431: gpio: add ErrorKind. r=Dirbaio a=Dirbaio This adds an `ErrorKind` to the GPIO `Error` associated type, just like all the other drivers. It has just a single `Other` variant for now. The reason to have it is future-proofness: Even if we don't have ideas for ErrorKind variants for now, we might have them in the future. - If we have `ErrorKind`, we can add more variants in the future backwards-compatibly after the 1.0 launch, thanks to `ErrorKind` being marked `#[non_exhaustive]`. - If we *don't* have `ErrorKind`, adding it in the future would be a breaking change, because HALs would have to add support for it. Co-authored-by: Dario Nieuwenhuis <[email protected]>
2 parents 010dc85 + bbd884e commit ef20420

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

embedded-hal/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
### Changed
11+
- gpio: add `ErrorKind` enum for consistency with other traits and for future extensibility. No kinds are defined for now.
1012

1113
## [v1.0.0-alpha.9] - 2022-09-28
1214

embedded-hal/src/digital.rs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,63 @@
22
33
use core::{convert::From, ops::Not};
44

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
651
///
752
/// This just defines the error type, to be used by the other traits.
853
pub trait ErrorType {
954
/// Error type
10-
type Error: core::fmt::Debug;
55+
type Error: Error;
1156
}
1257

1358
impl<T: ErrorType> ErrorType for &T {
1459
type Error = T::Error;
1560
}
61+
1662
impl<T: ErrorType> ErrorType for &mut T {
1763
type Error = T::Error;
1864
}

0 commit comments

Comments
 (0)