From 17b5b7024877fae88e4e58cfa1508d3f30e3e7d7 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Mon, 3 Jan 2022 09:01:36 +0100 Subject: [PATCH] digital: enforce all traits have the same Error type. --- CHANGELOG.md | 3 +++ src/digital.rs | 36 ++++++++++++++++++------------------ 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 656efa373..c635bdca1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) *** diff --git a/src/digital.rs b/src/digital.rs index e9266b6ec..b063d4f75 100644 --- a/src/digital.rs +++ b/src/digital.rs @@ -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 ErrorType for &T { + type Error = T::Error; +} +impl ErrorType for &mut T { + type Error = T::Error; +} + /// Digital output pin state /// /// Conversion from `bool` and logical negation are also implemented @@ -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 @@ -74,8 +86,6 @@ pub mod blocking { } impl OutputPin for &mut T { - type Error = T::Error; - fn set_low(&mut self) -> Result<(), Self::Error> { T::set_low(self) } @@ -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 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; @@ -142,8 +144,6 @@ pub mod blocking { } impl InputPin for &T { - type Error = T::Error; - fn is_high(&self) -> Result { T::is_high(self) }