Skip to content

InputPin instead of OutputPin for compatibility shim #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}

}