Skip to content

Commit 03e9784

Browse files
committed
Add an unwrapping adapter
1 parent 64ba1c3 commit 03e9784

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

embedded-hal-bus/src/lib.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,38 @@ use defmt_03 as defmt;
1818

1919
pub mod i2c;
2020
pub mod spi;
21+
22+
/// This adapter will [panic] if the inner device encounters an error.
23+
///
24+
/// It currently supports [embedded_hal::digital::OutputPin], but other traits may be added in the future.
25+
///
26+
/// TODO: add usage example
27+
#[repr(transparent)]
28+
pub struct UnwrappingAdapter<T>(pub T);
29+
30+
impl<T> embedded_hal::digital::ErrorType for UnwrappingAdapter<T> {
31+
type Error = core::convert::Infallible;
32+
}
33+
34+
impl<T> embedded_hal::digital::OutputPin for UnwrappingAdapter<T>
35+
where
36+
T: embedded_hal::digital::OutputPin,
37+
{
38+
#[inline]
39+
fn set_low(&mut self) -> Result<(), Self::Error> {
40+
self.0.set_low().unwrap();
41+
Ok(())
42+
}
43+
44+
#[inline]
45+
fn set_high(&mut self) -> Result<(), Self::Error> {
46+
self.0.set_high().unwrap();
47+
Ok(())
48+
}
49+
50+
#[inline]
51+
fn set_state(&mut self, state: embedded_hal::digital::PinState) -> Result<(), Self::Error> {
52+
self.0.set_state(state).unwrap();
53+
Ok(())
54+
}
55+
}

0 commit comments

Comments
 (0)