Skip to content

Commit

Permalink
Merge #199
Browse files Browse the repository at this point in the history
199: InputPin instead of OutputPin for compatibility shim r=ryankurte a=AdminXVII

The OldInputPin required OutputPin instead of InputPin for the new impl. Fix the trait bound.

Co-authored-by: Xavier L'Heureux <dev.xlheureux@gmail.com>
Co-authored-by: Ryan <ryankurte@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 7, 2020
2 parents 9e6ab5a + 6ccf55c commit e89399b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 36 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +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 `v1::OutputPin` values.

## [v0.2.3] - 2019-05-09

Expand Down
83 changes: 47 additions & 36 deletions src/digital/v1_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ pub struct OldOutputPin<T> {
pin: T,
}

impl <T, E> OldOutputPin<T>
impl<T, E> OldOutputPin<T>
where
T: v2::OutputPin<Error=E>,
T: v2::OutputPin<Error = E>,
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
Expand All @@ -29,22 +29,22 @@ where
}
}

impl <T, E> From<T> for OldOutputPin<T>
impl<T, E> From<T> for OldOutputPin<T>
where
T: v2::OutputPin<Error=E>,
T: v2::OutputPin<Error = E>,
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 <T, E> v1::OutputPin for OldOutputPin<T>
impl<T, E> v1::OutputPin for OldOutputPin<T>
where
T: v2::OutputPin<Error=E>,
T: v2::OutputPin<Error = E>,
E: core::fmt::Debug,
{
fn set_low(&mut self) {
Expand All @@ -60,9 +60,9 @@ where
/// where errors will panic.
#[cfg(feature = "unproven")]
#[allow(deprecated)]
impl <T, E> v1::StatefulOutputPin for OldOutputPin<T>
impl<T, E> v1::StatefulOutputPin for OldOutputPin<T>
where
T: v2::StatefulOutputPin<Error=E>,
T: v2::StatefulOutputPin<Error = E>,
E: core::fmt::Debug,
{
fn is_set_low(&self) -> bool {
Expand All @@ -82,36 +82,35 @@ pub struct OldInputPin<T> {
}

#[cfg(feature = "unproven")]
impl <T, E> OldInputPin<T>
impl<T, E> OldInputPin<T>
where
T: v2::OutputPin<Error=E>,
T: v2::InputPin<Error = E>,
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 <T, E> From<T> for OldInputPin<T>
impl<T, E> From<T> for OldInputPin<T>
where
T: v2::InputPin<Error=E>,
T: v2::InputPin<Error = E>,
E: core::fmt::Debug,
{
fn from(pin: T) -> Self {
OldInputPin{pin}
OldInputPin { pin }
}
}

/// Implementation of `v1::InputPin` trait for `v2::InputPin` fallible pins
/// where errors will panic.
#[cfg(feature = "unproven")]
#[allow(deprecated)]
impl <T, E> v1::InputPin for OldInputPin<T>
impl<T, E> v1::InputPin for OldInputPin<T>
where
T: v2::InputPin<Error=E>,
T: v2::InputPin<Error = E>,
E: core::fmt::Debug,
{
fn is_low(&self) -> bool {
Expand All @@ -137,7 +136,7 @@ mod tests {
#[derive(Clone)]
struct NewOutputPinImpl {
state: bool,
res: Result<(), ()>
res: Result<(), ()>,
}

impl v2::OutputPin for NewOutputPinImpl {
Expand All @@ -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
}
Expand All @@ -159,35 +158,47 @@ mod tests {
}

#[allow(deprecated)]
impl <T>OldOutputPinConsumer<T>
where T: v1::OutputPin
impl<T> OldOutputPinConsumer<T>
where
T: v1::OutputPin,
{
pub fn new(pin: T) -> OldOutputPinConsumer<T> {
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<OldOutputPin<_>> = 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();
}
Expand All @@ -207,7 +218,7 @@ mod tests {
fn is_low(&self) -> Result<bool, Self::Error> {
self.state.map(|v| v == false)
}
fn is_high(&self) -> Result<bool, Self::Error>{
fn is_high(&self) -> Result<bool, Self::Error> {
self.state.map(|v| v == true)
}
}
Expand All @@ -220,25 +231,26 @@ mod tests {

#[cfg(feature = "unproven")]
#[allow(deprecated)]
impl <T>OldInputPinConsumer<T>
where T: v1::InputPin
impl<T> OldInputPinConsumer<T>
where
T: v1::InputPin,
{
pub fn new(pin: T) -> OldInputPinConsumer<T> {
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<OldInputPin<_>> = 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);
Expand All @@ -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();
}

}

0 comments on commit e89399b

Please sign in to comment.