Skip to content

digital: enforce all traits have the same Error type. #338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed
- Fixed blanket impl of `DelayUs` not covering the `delay_ms` method.

### Changed
- `digital`: traits now enforce all impls on the same struct have the same `Error` type.

## [v1.0.0-alpha.6] - 2021-11-19

*** This is (also) an alpha release with breaking changes (sorry) ***
Expand Down
36 changes: 18 additions & 18 deletions src/digital.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

use core::{convert::From, ops::Not};

/// GPIO error type trait
///
/// This just defines the error type, to be used by the other traits.
pub trait ErrorType {
/// Error type
type Error: core::fmt::Debug;
}

impl<T: ErrorType> ErrorType for &T {
type Error = T::Error;
}
impl<T: ErrorType> ErrorType for &mut T {
type Error = T::Error;
}

/// Digital output pin state
///
/// Conversion from `bool` and logical negation are also implemented
Expand Down Expand Up @@ -45,10 +60,7 @@ pub mod blocking {
use super::PinState;

/// Single digital push-pull output pin
pub trait OutputPin {
/// Error type
type Error: core::fmt::Debug;

pub trait OutputPin: super::ErrorType {
/// Drives the pin low
///
/// *NOTE* the actual electrical state of the pin may not actually be low, e.g. due to external
Expand All @@ -74,8 +86,6 @@ pub mod blocking {
}

impl<T: OutputPin> OutputPin for &mut T {
type Error = T::Error;

fn set_low(&mut self) -> Result<(), Self::Error> {
T::set_low(self)
}
Expand Down Expand Up @@ -113,27 +123,19 @@ pub mod blocking {
}

/// Output pin that can be toggled
pub trait ToggleableOutputPin {
/// Error type
type Error: core::fmt::Debug;

pub trait ToggleableOutputPin: super::ErrorType {
/// Toggle pin output.
fn toggle(&mut self) -> Result<(), Self::Error>;
}

impl<T: ToggleableOutputPin> ToggleableOutputPin for &mut T {
type Error = T::Error;

fn toggle(&mut self) -> Result<(), Self::Error> {
T::toggle(self)
}
}

/// Single digital input pin
pub trait InputPin {
/// Error type
type Error: core::fmt::Debug;

pub trait InputPin: super::ErrorType {
/// Is the input pin high?
fn is_high(&self) -> Result<bool, Self::Error>;

Expand All @@ -142,8 +144,6 @@ pub mod blocking {
}

impl<T: InputPin> InputPin for &T {
type Error = T::Error;

fn is_high(&self) -> Result<bool, Self::Error> {
T::is_high(self)
}
Expand Down