Skip to content

Commit 5e21d56

Browse files
authored
Merge pull request #575 from qwandor/stderror
embedded-hal-bus: Implement `Display` and `std::error::Error` for `DeviceError` if possible.
2 parents 04b4f18 + aebcf0f commit 5e21d56

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

embedded-hal-bus/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ provides mechanisms to obtain multiple `I2c` instances out of a single `I2c` ins
3030

3131
## Optional Cargo features
3232

33-
- **`std`**: enable shared bus implementations using `std::sync::Mutex`.
33+
- **`std`**: enable shared bus implementations using `std::sync::Mutex`, and implement
34+
`std::error::Error` for `DeviceError`.
3435
- **`async`**: enable `embedded-hal-async` support.
3536
- **`defmt-03`**: Derive `defmt::Format` from `defmt` 0.3 for enums and structs.
3637

embedded-hal-bus/src/spi/mod.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! `SpiDevice` implementations.
22
3-
use core::fmt::Debug;
3+
use core::fmt::{self, Debug, Display, Formatter};
44
use embedded_hal::spi::{Error, ErrorKind};
55

66
mod exclusive;
@@ -29,6 +29,18 @@ pub enum DeviceError<BUS, CS> {
2929
Cs(CS),
3030
}
3131

32+
impl<BUS: Display, CS: Display> Display for DeviceError<BUS, CS> {
33+
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
34+
match self {
35+
Self::Spi(bus) => write!(f, "SPI bus error: {}", bus),
36+
Self::Cs(cs) => write!(f, "SPI CS error: {}", cs),
37+
}
38+
}
39+
}
40+
41+
#[cfg(feature = "std")]
42+
impl<BUS: Debug + Display, CS: Debug + Display> std::error::Error for DeviceError<BUS, CS> {}
43+
3244
impl<BUS, CS> Error for DeviceError<BUS, CS>
3345
where
3446
BUS: Error + Debug,

0 commit comments

Comments
 (0)