Skip to content
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

Digital v1 <-> v2 compatibility wrappers and shims #127

Merged
merged 11 commits into from
Mar 21, 2019
111 changes: 81 additions & 30 deletions src/digital/v1_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,26 @@ pub struct OldOutputPin<T> {
pin: T,
}

impl <T, ERR> OldOutputPin<T>
impl <T, E> OldOutputPin<T>
where
T: v2::OutputPin<Error=ERR>,
ERR: core::fmt::Debug,
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}
}

/// Fetch a reference to the inner v2::OutputPin impl
pub fn inner(&self) -> &T {
ryankurte marked this conversation as resolved.
Show resolved Hide resolved
&self.pin
}
}

impl <T, ERR> From<T> for OldOutputPin<T>
impl <T, E> From<T> for OldOutputPin<T>
where
T: v2::OutputPin<Error=ERR>,
ERR: core::fmt::Debug,
T: v2::OutputPin<Error=E>,
E: core::fmt::Debug,
{
fn from(pin: T) -> Self {
OldOutputPin{pin}
Expand All @@ -34,10 +39,10 @@ where

/// Implementation of v1 OutputPin trait for v2 fallible output pins
ryankurte marked this conversation as resolved.
Show resolved Hide resolved
#[allow(deprecated)]
impl <T, ERR> v1::OutputPin for OldOutputPin<T>
impl <T, E> v1::OutputPin for OldOutputPin<T>
where
T: v2::OutputPin<Error=ERR>,
ERR: core::fmt::Debug,
T: v2::OutputPin<Error=E>,
E: core::fmt::Debug,
{
fn set_low(&mut self) {
self.pin.set_low().unwrap()
Expand All @@ -51,10 +56,10 @@ where
/// Implementation of v1 StatefulOutputPin trait for v2 fallible pins
ryankurte marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(feature = "unproven")]
#[allow(deprecated)]
impl <T, ERR> v1::StatefulOutputPin for OldOutputPin<T>
impl <T, E> v1::StatefulOutputPin for OldOutputPin<T>
where
T: v2::StatefulOutputPin<Error=ERR>,
ERR: core::fmt::Debug,
T: v2::StatefulOutputPin<Error=E>,
E: core::fmt::Debug,
{
fn is_set_low(&self) -> bool {
self.pin.is_set_low().unwrap()
Expand All @@ -72,22 +77,27 @@ pub struct OldInputPin<T> {
}

#[cfg(feature = "unproven")]
impl <T, ERR> OldInputPin<T>
impl <T, E> OldInputPin<T>
where
T: v2::OutputPin<Error=ERR>,
ERR: core::fmt::Debug,
T: v2::OutputPin<Error=E>,
E: core::fmt::Debug,
{
/// Create an OldInputPin wrapper around a v2::InputPin
ryankurte marked this conversation as resolved.
Show resolved Hide resolved
pub fn new(pin: T) -> Self {
Self{pin}
}

/// Fetch a reference to the inner v2::InputPin impl
pub fn inner(&self) -> &T {
&self.pin
}
}

#[cfg(feature = "unproven")]
impl <T, ERR> From<T> for OldInputPin<T>
impl <T, E> From<T> for OldInputPin<T>
where
T: v2::InputPin<Error=ERR>,
ERR: core::fmt::Debug,
T: v2::InputPin<Error=E>,
E: core::fmt::Debug,
{
fn from(pin: T) -> Self {
OldInputPin{pin}
Expand All @@ -97,10 +107,10 @@ where
/// Implementation of v0.2 InputPin trait for v0.3 fallible pins
ryankurte marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(feature = "unproven")]
#[allow(deprecated)]
impl <T, ERR> v1::InputPin for OldInputPin<T>
impl <T, E> v1::InputPin for OldInputPin<T>
where
T: v2::InputPin<Error=ERR>,
ERR: core::fmt::Debug,
T: v2::InputPin<Error=E>,
E: core::fmt::Debug,
{
fn is_low(&self) -> bool {
self.pin.is_low().unwrap()
Expand All @@ -120,20 +130,24 @@ mod tests {
use crate::digital::v1;
use crate::digital::v2;

struct NewOutputPinImpl {
state: bool
use crate::digital::v1::{InputPin, OutputPin};

#[derive(Clone)]
struct NewOutputPinImpl {
state: bool,
res: Result<(), ()>
}

impl v2::OutputPin for NewOutputPinImpl {
type Error = ();

fn set_low(&mut self) -> Result<(), Self::Error> {
self.state = false;
Ok(())
self.res
}
fn set_high(&mut self) -> Result<(), Self::Error>{
self.state = true;
Ok(())
self.res
}
}

Expand All @@ -153,24 +167,43 @@ mod tests {

#[test]
fn v1_v2_output_explicit() {
let i = NewOutputPinImpl{state: false};
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();

o.set_high();
assert_eq!(o.inner().state, true);

o.set_low();
assert_eq!(o.inner().state, false);
}

#[test]
#[should_panic]
fn v1_v2_output_panic() {
let mut o: OldOutputPin<_> = NewOutputPinImpl{state: false, res: Err(())}.into();

o.set_high();
}

#[cfg(feature = "unproven")]
struct NewInputPinImpl {
state: bool,
state: Result<bool, ()>,
}

#[cfg(feature = "unproven")]
impl v2::InputPin for NewInputPinImpl {
type Error = ();

fn is_low(&self) -> Result<bool, Self::Error> {
Ok(!self.state)
self.state.map(|v| v == false)
}
fn is_high(&self) -> Result<bool, Self::Error>{
Ok(self.state)
self.state.map(|v| v == true)
}
}

Expand All @@ -193,8 +226,26 @@ mod tests {
#[cfg(feature = "unproven")]
#[test]
fn v1_v2_input_explicit() {
let i = NewInputPinImpl{state: false};
let i = NewInputPinImpl{state: Ok(false)};
let _c: OldInputPinConsumer<OldInputPin<_>> = OldInputPinConsumer::new(i.into());
}

ryankurte marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(feature = "unproven")]
#[test]
fn v1_v2_input_state() {
let i: OldInputPin<_> = NewInputPinImpl{state: Ok(false)}.into();

assert_eq!(i.is_low(), true);
assert_eq!(i.is_high(), false);
}

#[cfg(feature = "unproven")]
#[test]
#[should_panic]
fn v1_v2_input_panic() {
let i: OldInputPin<_> = NewInputPinImpl{state: Err(())}.into();

i.is_low();
}

}
25 changes: 23 additions & 2 deletions src/digital/v2_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ impl <T> v2::OutputPin for T
where
T: v1::OutputPin,
{
// TODO: update to ! when never_type is stabilized
type Error = ();

/// Toggle pin output
Expand Down Expand Up @@ -46,6 +47,7 @@ impl <T> v2::InputPin for T
where
T: v1::InputPin
{
// TODO: update to ! when never_type is stabilized
type Error = ();

/// Toggle pin output
Expand Down Expand Up @@ -97,6 +99,17 @@ mod tests {
let _c = NewOutputPinConsumer::new(i);
}

#[test]
fn v2_v1_output_state() {
let mut o = OldOutputPinImpl{state: false};

v2::OutputPin::set_high(&mut o).unwrap();
assert_eq!(o.state, true);

v2::OutputPin::set_low(&mut o).unwrap();
assert_eq!(o.state, false);
}

#[cfg(feature = "unproven")]
#[allow(deprecated)]
struct OldInputPinImpl {
Expand Down Expand Up @@ -130,8 +143,16 @@ mod tests {
#[cfg(feature = "unproven")]
#[test]
fn v2_v1_input_implicit() {
let i = OldOutputPinImpl{state: false};
let _c = NewOutputPinConsumer::new(i);
let i = OldInputPinImpl{state: false};
let _c = NewInputPinConsumer::new(i);
}

#[cfg(feature = "unproven")]
#[test]
fn v2_v1_input_state() {
let mut i = OldInputPinImpl{state: false};

assert_eq!(v2::InputPin::is_high(&mut i).unwrap(), false);
assert_eq!(v2::InputPin::is_low(&mut i).unwrap(), true);
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@
//! ```

#![deny(missing_docs)]
//#![deny(warnings)]
#![deny(warnings)]
#![no_std]

#[macro_use]
Expand Down