From 5708d979d8e0f7ea3bfaa4f07fea0a56d026817a Mon Sep 17 00:00:00 2001 From: Roma Sokolov Date: Sun, 12 May 2019 19:15:12 +0200 Subject: [PATCH 1/5] Provide v2_compat for ToggleableOutputPin fixes #137 --- src/digital/v2_compat.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/digital/v2_compat.rs b/src/digital/v2_compat.rs index 779c089d3..bb5bae8b5 100644 --- a/src/digital/v2_compat.rs +++ b/src/digital/v2_compat.rs @@ -39,6 +39,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 +84,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 +109,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}; From 45324110519eb61e26f810580092593294e47b20 Mon Sep 17 00:00:00 2001 From: Ryan Kurte Date: Sun, 12 May 2019 10:56:57 +1200 Subject: [PATCH 2/5] improved digital compatibility docs --- src/digital/v1_compat.rs | 40 ++++++++++++++++++++++++++++++++++++---- src/digital/v2_compat.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 4 deletions(-) diff --git a/src/digital/v1_compat.rs b/src/digital/v1_compat.rs index aed9aaf7a..7ac6f544d 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; diff --git a/src/digital/v2_compat.rs b/src/digital/v2_compat.rs index bb5bae8b5..f88d8d38a 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 +//! 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; From b1046883289984a769c53d62e76c5dd0c226cda0 Mon Sep 17 00:00:00 2001 From: Ryan Kurte Date: Sun, 26 May 2019 11:48:07 +1200 Subject: [PATCH 3/5] added capitals --- src/digital/v1_compat.rs | 2 +- src/digital/v2_compat.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/digital/v1_compat.rs b/src/digital/v1_compat.rs index 7ac6f544d..9de6e93ee 100644 --- a/src/digital/v1_compat.rs +++ b/src/digital/v1_compat.rs @@ -1,6 +1,6 @@ //! v1 compatibility wrappers //! -//! this module provides wrappers to support use of v2 implementations with +//! 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 //! diff --git a/src/digital/v2_compat.rs b/src/digital/v2_compat.rs index f88d8d38a..c96fee8b3 100644 --- a/src/digital/v2_compat.rs +++ b/src/digital/v2_compat.rs @@ -1,6 +1,6 @@ //! 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. //! //! ``` From 59f33ca1a13d7da3f55eaa59a0ae656e9d819108 Mon Sep 17 00:00:00 2001 From: Diego Barrios Romero Date: Wed, 10 Jun 2020 08:39:44 +0200 Subject: [PATCH 4/5] Fix using InputPin instead of OutputPin Taken from https://github.com/rust-embedded/embedded-hal/pull/199 --- src/digital/v1_compat.rs | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/digital/v1_compat.rs b/src/digital/v1_compat.rs index 9de6e93ee..bb3430f58 100644 --- a/src/digital/v1_compat.rs +++ b/src/digital/v1_compat.rs @@ -1,38 +1,38 @@ //! 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 +//! 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 +//! +//! 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)] @@ -92,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, @@ -116,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`. @@ -191,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 } @@ -213,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] @@ -252,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 } From 55461c3bf751a7f3961b543e6f2cfcd2e86ccb53 Mon Sep 17 00:00:00 2001 From: Diego Barrios Romero Date: Wed, 10 Jun 2020 08:40:14 +0200 Subject: [PATCH 5/5] Update changelog with text from #199 --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) 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