diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d7261970..b0b7fcd37 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Changed +- Fix the input pin v2->v1 compatibility shim constructor, where `OldInputPin::new` + was incorrectly implemented for `v1::OutputPin` values. + ## [v0.2.3] - 2019-05-09 diff --git a/src/digital/v1_compat.rs b/src/digital/v1_compat.rs index aed9aaf7a..bb3430f58 100644 --- a/src/digital/v1_compat.rs +++ b/src/digital/v1_compat.rs @@ -1,7 +1,39 @@ -//! v1 compatibility wrapper -//! this module adds reverse support for v2 digital traits -//! v2 traits must be explicitly cast to the v1 version using `.into()`, -//! and will panic on internal errors +//! v1 compatibility wrappers +//! +//! This module provides wrappers to support use of v2 implementations with +//! v1 consumers. v2 traits must be explicitly cast to the v1 version using +//! `.into()`, and will panic on internal errors +//! +//! ``` +//! extern crate embedded_hal; +//! use embedded_hal::digital::{v1, v2, v1_compat::OldOutputPin}; +//! +//! struct NewOutputPinImpl {} +//! +//! impl v2::OutputPin for NewOutputPinImpl { +//! type Error = (); +//! fn set_low(&mut self) -> Result<(), Self::Error> { Ok(()) } +//! fn set_high(&mut self) -> Result<(), Self::Error>{ Ok(()) } +//! } +//! +//! struct OldOutputPinConsumer { +//! _pin: T, +//! } +//! +//! impl OldOutputPinConsumer +//! where T: v1::OutputPin { +//! pub fn new(pin: T) -> OldOutputPinConsumer { +//! OldOutputPinConsumer{ _pin: pin } +//! } +//! } +//! +//! fn main() { +//! let pin = NewOutputPinImpl{}; +//! let _consumer: OldOutputPinConsumer> = OldOutputPinConsumer::new(pin.into()); +//! } +//! ``` +//! + #[allow(deprecated)] use super::v1; @@ -60,7 +92,7 @@ where /// where errors will panic. #[cfg(feature = "unproven")] #[allow(deprecated)] -impl v1::StatefulOutputPin for OldOutputPin +impl v1::StatefulOutputPin for OldOutputPin where T: v2::StatefulOutputPin, E: core::fmt::Debug, @@ -84,7 +116,7 @@ pub struct OldInputPin { #[cfg(feature = "unproven")] impl OldInputPin where - T: v2::OutputPin, + T: v2::InputPin, E: core::fmt::Debug, { /// Create an `OldInputPin` wrapper around a `v2::InputPin`. @@ -159,8 +191,8 @@ mod tests { } #[allow(deprecated)] - impl OldOutputPinConsumer - where T: v1::OutputPin + impl OldOutputPinConsumer + where T: v1::OutputPin { pub fn new(pin: T) -> OldOutputPinConsumer { OldOutputPinConsumer{ _pin: pin } @@ -181,7 +213,7 @@ mod tests { assert_eq!(o.inner().state, true); o.set_low(); - assert_eq!(o.inner().state, false); + assert_eq!(o.inner().state, false); } #[test] @@ -220,8 +252,8 @@ mod tests { #[cfg(feature = "unproven")] #[allow(deprecated)] - impl OldInputPinConsumer - where T: v1::InputPin + impl OldInputPinConsumer + where T: v1::InputPin { pub fn new(pin: T) -> OldInputPinConsumer { OldInputPinConsumer{ _pin: pin } diff --git a/src/digital/v2_compat.rs b/src/digital/v2_compat.rs index 779c089d3..c96fee8b3 100644 --- a/src/digital/v2_compat.rs +++ b/src/digital/v2_compat.rs @@ -1,5 +1,36 @@ //! v2 compatibility shims -//! this module adds implicit forward support to v1 digital traits +//! +//! This module adds implicit forward support to v1 digital traits, +//! allowing v1 implementations to be directly used with v2 consumers. +//! +//! ``` +//! extern crate embedded_hal; +//! use embedded_hal::digital::{v1, v2}; +//! +//! struct OldOutputPinImpl { } +//! +//! impl v1::OutputPin for OldOutputPinImpl { +//! fn set_low(&mut self) { } +//! fn set_high(&mut self) { } +//! } +//! +//! struct NewOutputPinConsumer { +//! _pin: T, +//! } +//! +//! impl NewOutputPinConsumer +//! where T: v2::OutputPin { +//! pub fn new(pin: T) -> NewOutputPinConsumer { +//! NewOutputPinConsumer{ _pin: pin } +//! } +//! } +//! +//! fn main() { +//! let pin = OldOutputPinImpl{}; +//! let _consumer = NewOutputPinConsumer::new(pin); +//! } +//! ``` +//! #[allow(deprecated)] use super::v1; @@ -39,6 +70,9 @@ where } } +#[cfg(feature = "unproven")] +#[allow(deprecated)] +impl v2::toggleable::Default for T where T: v1::toggleable::Default {} /// Implementation of fallible `v2::InputPin` for `v1::InputPin` digital traits #[cfg(feature = "unproven")] @@ -81,6 +115,20 @@ mod tests { } } + #[allow(deprecated)] + impl v1::StatefulOutputPin for OldOutputPinImpl { + fn is_set_low(&self) -> bool { + self.state == false + } + + fn is_set_high(&self) -> bool { + self.state == true + } + } + + #[allow(deprecated)] + impl v1::toggleable::Default for OldOutputPinImpl {} + struct NewOutputPinConsumer { _pin: T, } @@ -92,6 +140,25 @@ mod tests { } } + struct NewToggleablePinConsumer { + _pin: T, + } + + impl NewToggleablePinConsumer + where + T: v2::ToggleableOutputPin, + { + pub fn new(pin: T) -> NewToggleablePinConsumer { + NewToggleablePinConsumer { _pin: pin } + } + } + + #[test] + fn v2_v1_toggleable_implicit() { + let i = OldOutputPinImpl { state: false }; + let _c = NewToggleablePinConsumer::new(i); + } + #[test] fn v2_v1_output_implicit() { let i = OldOutputPinImpl{state: false};