Skip to content

Commit 7f18e18

Browse files
committed
digital: enforce all traits have the same Error type.
1 parent 0238b94 commit 7f18e18

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1313
### Fixed
1414
- Fixed blanket impl of `DelayUs` not covering the `delay_ms` method.
1515

16+
### Changed
17+
- `digital`: traits now enforce all impls on the same struct have the same `Error` type.
18+
1619
## [v1.0.0-alpha.6] - 2021-11-19
1720

1821
*** This is (also) an alpha release with breaking changes (sorry) ***

src/digital.rs

+21-24
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@
22
33
use core::{convert::From, ops::Not};
44

5+
/// GPIO error type trait
6+
///
7+
/// This just defines the error type, to be used by the other traits.
8+
pub trait ErrorType {
9+
/// Error type
10+
type Error: core::fmt::Debug;
11+
}
12+
13+
impl<T: ErrorType> ErrorType for &T {
14+
type Error = T::Error;
15+
}
16+
impl<T: ErrorType> ErrorType for &mut T {
17+
type Error = T::Error;
18+
}
19+
520
/// Digital output pin state
621
///
722
/// Conversion from `bool` and logical negation are also implemented
@@ -45,10 +60,7 @@ pub mod blocking {
4560
use super::PinState;
4661

4762
/// Single digital push-pull output pin
48-
pub trait OutputPin {
49-
/// Error type
50-
type Error: core::fmt::Debug;
51-
63+
pub trait OutputPin: super::ErrorType {
5264
/// Drives the pin low
5365
///
5466
/// *NOTE* the actual electrical state of the pin may not actually be low, e.g. due to external
@@ -74,8 +86,6 @@ pub mod blocking {
7486
}
7587

7688
impl<T: OutputPin> OutputPin for &mut T {
77-
type Error = T::Error;
78-
7989
fn set_low(&mut self) -> Result<(), Self::Error> {
8090
T::set_low(self)
8191
}
@@ -113,27 +123,19 @@ pub mod blocking {
113123
}
114124

115125
/// Output pin that can be toggled
116-
pub trait ToggleableOutputPin {
117-
/// Error type
118-
type Error: core::fmt::Debug;
119-
126+
pub trait ToggleableOutputPin: super::ErrorType {
120127
/// Toggle pin output.
121128
fn toggle(&mut self) -> Result<(), Self::Error>;
122129
}
123130

124131
impl<T: ToggleableOutputPin> ToggleableOutputPin for &mut T {
125-
type Error = T::Error;
126-
127132
fn toggle(&mut self) -> Result<(), Self::Error> {
128133
T::toggle(self)
129134
}
130135
}
131136

132137
/// Single digital input pin
133-
pub trait InputPin {
134-
/// Error type
135-
type Error: core::fmt::Debug;
136-
138+
pub trait InputPin: super::ErrorType {
137139
/// Is the input pin high?
138140
fn is_high(&self) -> Result<bool, Self::Error>;
139141

@@ -142,8 +144,6 @@ pub mod blocking {
142144
}
143145

144146
impl<T: InputPin> InputPin for &T {
145-
type Error = T::Error;
146-
147147
fn is_high(&self) -> Result<bool, Self::Error> {
148148
T::is_high(self)
149149
}
@@ -179,14 +179,11 @@ pub mod blocking {
179179
/// pin.is_high()
180180
/// }
181181
/// ```
182-
pub trait IoPin<TInput, TOutput>
182+
pub trait IoPin<TInput, TOutput>: super::ErrorType
183183
where
184-
TInput: InputPin + IoPin<TInput, TOutput>,
185-
TOutput: OutputPin + IoPin<TInput, TOutput>,
184+
TInput: super::ErrorType<Error = Self::Error> + InputPin + IoPin<TInput, TOutput>,
185+
TOutput: super::ErrorType<Error = Self::Error> + OutputPin + IoPin<TInput, TOutput>,
186186
{
187-
/// Error type.
188-
type Error: core::fmt::Debug;
189-
190187
/// Tries to convert this pin to input mode.
191188
///
192189
/// If the pin is already in input mode, this method should succeed.

0 commit comments

Comments
 (0)