From 7e87fab03734e8bc487d01c58b03c987f3e52818 Mon Sep 17 00:00:00 2001 From: Xavier L'Heureux Date: Mon, 6 Apr 2020 20:09:19 -0400 Subject: [PATCH 1/4] InputPin instead of OutputPin for compatibility shim The OldInputPin required OutputPin instead of InputPin for the new impl. Fix the trait bound. --- CHANGELOG.md | 2 + src/digital/v1_compat.rs | 83 +++++++++++++++++++++++----------------- 2 files changed, 49 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d7261970..717145dec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Changed +- Fix the compatibility shim, where InputPin was implemented only for inner v1::OutputPin + values, a potentially serious problem. ## [v0.2.3] - 2019-05-09 diff --git a/src/digital/v1_compat.rs b/src/digital/v1_compat.rs index aed9aaf7a..0d9b9d647 100644 --- a/src/digital/v1_compat.rs +++ b/src/digital/v1_compat.rs @@ -12,14 +12,14 @@ pub struct OldOutputPin { pin: T, } -impl OldOutputPin +impl OldOutputPin where - T: v2::OutputPin, + T: v2::OutputPin, E: core::fmt::Debug, { /// Create a new OldOutputPin wrapper around a `v2::OutputPin` pub fn new(pin: T) -> Self { - Self{pin} + Self { pin } } /// Fetch a reference to the inner `v2::OutputPin` impl @@ -29,22 +29,22 @@ where } } -impl From for OldOutputPin +impl From for OldOutputPin where - T: v2::OutputPin, + T: v2::OutputPin, E: core::fmt::Debug, { fn from(pin: T) -> Self { - OldOutputPin{pin} + OldOutputPin { pin } } } /// Implementation of `v1::OutputPin` trait for fallible `v2::OutputPin` output pins /// where errors will panic. #[allow(deprecated)] -impl v1::OutputPin for OldOutputPin +impl v1::OutputPin for OldOutputPin where - T: v2::OutputPin, + T: v2::OutputPin, E: core::fmt::Debug, { fn set_low(&mut self) { @@ -60,9 +60,9 @@ where /// where errors will panic. #[cfg(feature = "unproven")] #[allow(deprecated)] -impl v1::StatefulOutputPin for OldOutputPin +impl v1::StatefulOutputPin for OldOutputPin where - T: v2::StatefulOutputPin, + T: v2::StatefulOutputPin, E: core::fmt::Debug, { fn is_set_low(&self) -> bool { @@ -82,26 +82,25 @@ pub struct OldInputPin { } #[cfg(feature = "unproven")] -impl OldInputPin +impl OldInputPin where - T: v2::OutputPin, + T: v2::InputPin, E: core::fmt::Debug, { /// Create an `OldInputPin` wrapper around a `v2::InputPin`. pub fn new(pin: T) -> Self { - Self{pin} + Self { pin } } - } #[cfg(feature = "unproven")] -impl From for OldInputPin +impl From for OldInputPin where - T: v2::InputPin, + T: v2::InputPin, E: core::fmt::Debug, { fn from(pin: T) -> Self { - OldInputPin{pin} + OldInputPin { pin } } } @@ -109,9 +108,9 @@ where /// where errors will panic. #[cfg(feature = "unproven")] #[allow(deprecated)] -impl v1::InputPin for OldInputPin +impl v1::InputPin for OldInputPin where - T: v2::InputPin, + T: v2::InputPin, E: core::fmt::Debug, { fn is_low(&self) -> bool { @@ -137,7 +136,7 @@ mod tests { #[derive(Clone)] struct NewOutputPinImpl { state: bool, - res: Result<(), ()> + res: Result<(), ()>, } impl v2::OutputPin for NewOutputPinImpl { @@ -147,7 +146,7 @@ mod tests { self.state = false; self.res } - fn set_high(&mut self) -> Result<(), Self::Error>{ + fn set_high(&mut self) -> Result<(), Self::Error> { self.state = true; self.res } @@ -159,35 +158,47 @@ 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 } + OldOutputPinConsumer { _pin: pin } } } #[test] fn v1_v2_output_explicit() { - let i = NewOutputPinImpl{state: false, res: Ok(())}; + let i = NewOutputPinImpl { + state: false, + res: Ok(()), + }; let _c: OldOutputPinConsumer> = OldOutputPinConsumer::new(i.into()); } #[test] fn v1_v2_output_state() { - let mut o: OldOutputPin<_> = NewOutputPinImpl{state: false, res: Ok(())}.into(); + let mut o: OldOutputPin<_> = NewOutputPinImpl { + state: false, + res: Ok(()), + } + .into(); o.set_high(); assert_eq!(o.inner().state, true); o.set_low(); - assert_eq!(o.inner().state, false); + assert_eq!(o.inner().state, false); } #[test] #[should_panic] fn v1_v2_output_panic() { - let mut o: OldOutputPin<_> = NewOutputPinImpl{state: false, res: Err(())}.into(); + let mut o: OldOutputPin<_> = NewOutputPinImpl { + state: false, + res: Err(()), + } + .into(); o.set_high(); } @@ -207,7 +218,7 @@ mod tests { fn is_low(&self) -> Result { self.state.map(|v| v == false) } - fn is_high(&self) -> Result{ + fn is_high(&self) -> Result { self.state.map(|v| v == true) } } @@ -220,25 +231,26 @@ 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 } + OldInputPinConsumer { _pin: pin } } } #[cfg(feature = "unproven")] #[test] fn v1_v2_input_explicit() { - let i = NewInputPinImpl{state: Ok(false)}; + let i = NewInputPinImpl { state: Ok(false) }; let _c: OldInputPinConsumer> = OldInputPinConsumer::new(i.into()); } #[cfg(feature = "unproven")] #[test] fn v1_v2_input_state() { - let i: OldInputPin<_> = NewInputPinImpl{state: Ok(false)}.into(); + let i: OldInputPin<_> = NewInputPinImpl { state: Ok(false) }.into(); assert_eq!(i.is_low(), true); assert_eq!(i.is_high(), false); @@ -248,9 +260,8 @@ mod tests { #[test] #[should_panic] fn v1_v2_input_panic() { - let i: OldInputPin<_> = NewInputPinImpl{state: Err(())}.into(); + let i: OldInputPin<_> = NewInputPinImpl { state: Err(()) }.into(); i.is_low(); } - } From d6801ca5645fa71b5d9defddc319815ca9641e90 Mon Sep 17 00:00:00 2001 From: Ryan Date: Wed, 8 Apr 2020 09:03:38 +1200 Subject: [PATCH 2/4] Update CHANGELOG.md --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 717145dec..4139d41e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,8 +12,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Changed -- Fix the compatibility shim, where InputPin was implemented only for inner v1::OutputPin - values, a potentially serious problem. +- Fix the input pin v2->v1 compatibility shim constructor, where `OldInputPin::new` was incorrectly implemented for v`1::OutputPin` + values. ## [v0.2.3] - 2019-05-09 From bbb92deaa94c00da0f918fa74981453df18db19c Mon Sep 17 00:00:00 2001 From: Ryan Date: Wed, 8 Apr 2020 09:04:00 +1200 Subject: [PATCH 3/4] Update CHANGELOG.md --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4139d41e8..54b3fd376 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,8 +12,8 @@ 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 v`1::OutputPin` - values. +- Fix the input pin v2->v1 compatibility shim constructor, where `OldInputPin::new` + was incorrectly implemented for v`1::OutputPin` values. ## [v0.2.3] - 2019-05-09 From 6ccf55c9cbd266b9948933874ce052f341a33edb Mon Sep 17 00:00:00 2001 From: Ryan Date: Wed, 8 Apr 2020 09:04:16 +1200 Subject: [PATCH 4/4] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54b3fd376..72250fd29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,7 @@ 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 v`1::OutputPin` values. + was incorrectly implemented for `v1::OutputPin` values. ## [v0.2.3] - 2019-05-09