Skip to content

Commit bae5bcb

Browse files
bors[bot]Dirbaio
andauthored
Merge #338
338: digital: enforce all traits have the same Error type. r=therealprof a=Dirbaio Equivalent of #331 for GPIO traits. This one is a bit trickier, so it maybe warrants some discussion (this is the reason I've opened everything as separate PR's, sorry for the spam!): - The bounds on `IoPin` are becoming very cursed, but I *think* they're correct... Is there some HAL out there implementing it, so that I can verify this doesn't break it? - This forces the "input" and "output" errors to be the same. Perhaps we want to only unify Output and Input separately? In practice I don't think this will be an issue, as structs usually impl only Input or Output but not both (except with IoPin maybe?) Co-authored-by: Dario Nieuwenhuis <[email protected]>
2 parents 73c54d9 + 17b5b70 commit bae5bcb

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1515
### Changed
1616
- `spi`: traits now enforce all impls on the same struct (eg `Transfer` and `Write`) have the same `Error` type.
1717

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

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

src/digital.rs

+18-18
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
}

0 commit comments

Comments
 (0)