Skip to content

Commit f8e85ca

Browse files
committed
made suggested changes / improved and fixed tests
1 parent e3c4d12 commit f8e85ca

File tree

3 files changed

+105
-33
lines changed

3 files changed

+105
-33
lines changed

src/digital/v1_compat.rs

Lines changed: 81 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,26 @@ pub struct OldOutputPin<T> {
1111
pin: T,
1212
}
1313

14-
impl <T, ERR> OldOutputPin<T>
14+
impl <T, E> OldOutputPin<T>
1515
where
16-
T: v2::OutputPin<Error=ERR>,
17-
ERR: core::fmt::Debug,
16+
T: v2::OutputPin<Error=E>,
17+
E: core::fmt::Debug,
1818
{
1919
/// Create a new OldOutputPin wrapper around a v2::OutputPin
2020
pub fn new(pin: T) -> Self {
2121
Self{pin}
2222
}
23+
24+
/// Fetch a reference to the inner v2::OutputPin impl
25+
pub fn inner(&self) -> &T {
26+
&self.pin
27+
}
2328
}
2429

25-
impl <T, ERR> From<T> for OldOutputPin<T>
30+
impl <T, E> From<T> for OldOutputPin<T>
2631
where
27-
T: v2::OutputPin<Error=ERR>,
28-
ERR: core::fmt::Debug,
32+
T: v2::OutputPin<Error=E>,
33+
E: core::fmt::Debug,
2934
{
3035
fn from(pin: T) -> Self {
3136
OldOutputPin{pin}
@@ -34,10 +39,10 @@ where
3439

3540
/// Implementation of v1 OutputPin trait for v2 fallible output pins
3641
#[allow(deprecated)]
37-
impl <T, ERR> v1::OutputPin for OldOutputPin<T>
42+
impl <T, E> v1::OutputPin for OldOutputPin<T>
3843
where
39-
T: v2::OutputPin<Error=ERR>,
40-
ERR: core::fmt::Debug,
44+
T: v2::OutputPin<Error=E>,
45+
E: core::fmt::Debug,
4146
{
4247
fn set_low(&mut self) {
4348
self.pin.set_low().unwrap()
@@ -51,10 +56,10 @@ where
5156
/// Implementation of v1 StatefulOutputPin trait for v2 fallible pins
5257
#[cfg(feature = "unproven")]
5358
#[allow(deprecated)]
54-
impl <T, ERR> v1::StatefulOutputPin for OldOutputPin<T>
59+
impl <T, E> v1::StatefulOutputPin for OldOutputPin<T>
5560
where
56-
T: v2::StatefulOutputPin<Error=ERR>,
57-
ERR: core::fmt::Debug,
61+
T: v2::StatefulOutputPin<Error=E>,
62+
E: core::fmt::Debug,
5863
{
5964
fn is_set_low(&self) -> bool {
6065
self.pin.is_set_low().unwrap()
@@ -72,22 +77,27 @@ pub struct OldInputPin<T> {
7277
}
7378

7479
#[cfg(feature = "unproven")]
75-
impl <T, ERR> OldInputPin<T>
80+
impl <T, E> OldInputPin<T>
7681
where
77-
T: v2::OutputPin<Error=ERR>,
78-
ERR: core::fmt::Debug,
82+
T: v2::OutputPin<Error=E>,
83+
E: core::fmt::Debug,
7984
{
8085
/// Create an OldInputPin wrapper around a v2::InputPin
8186
pub fn new(pin: T) -> Self {
8287
Self{pin}
8388
}
89+
90+
/// Fetch a reference to the inner v2::InputPin impl
91+
pub fn inner(&self) -> &T {
92+
&self.pin
93+
}
8494
}
8595

8696
#[cfg(feature = "unproven")]
87-
impl <T, ERR> From<T> for OldInputPin<T>
97+
impl <T, E> From<T> for OldInputPin<T>
8898
where
89-
T: v2::InputPin<Error=ERR>,
90-
ERR: core::fmt::Debug,
99+
T: v2::InputPin<Error=E>,
100+
E: core::fmt::Debug,
91101
{
92102
fn from(pin: T) -> Self {
93103
OldInputPin{pin}
@@ -97,10 +107,10 @@ where
97107
/// Implementation of v0.2 InputPin trait for v0.3 fallible pins
98108
#[cfg(feature = "unproven")]
99109
#[allow(deprecated)]
100-
impl <T, ERR> v1::InputPin for OldInputPin<T>
110+
impl <T, E> v1::InputPin for OldInputPin<T>
101111
where
102-
T: v2::InputPin<Error=ERR>,
103-
ERR: core::fmt::Debug,
112+
T: v2::InputPin<Error=E>,
113+
E: core::fmt::Debug,
104114
{
105115
fn is_low(&self) -> bool {
106116
self.pin.is_low().unwrap()
@@ -120,20 +130,24 @@ mod tests {
120130
use crate::digital::v1;
121131
use crate::digital::v2;
122132

123-
struct NewOutputPinImpl {
124-
state: bool
133+
use crate::digital::v1::{InputPin, OutputPin};
134+
135+
#[derive(Clone)]
136+
struct NewOutputPinImpl {
137+
state: bool,
138+
res: Result<(), ()>
125139
}
126140

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

130144
fn set_low(&mut self) -> Result<(), Self::Error> {
131145
self.state = false;
132-
Ok(())
146+
self.res
133147
}
134148
fn set_high(&mut self) -> Result<(), Self::Error>{
135149
self.state = true;
136-
Ok(())
150+
self.res
137151
}
138152
}
139153

@@ -153,24 +167,43 @@ mod tests {
153167

154168
#[test]
155169
fn v1_v2_output_explicit() {
156-
let i = NewOutputPinImpl{state: false};
170+
let i = NewOutputPinImpl{state: false, res: Ok(())};
157171
let _c: OldOutputPinConsumer<OldOutputPin<_>> = OldOutputPinConsumer::new(i.into());
158172
}
159173

174+
#[test]
175+
fn v1_v2_output_state() {
176+
let mut o: OldOutputPin<_> = NewOutputPinImpl{state: false, res: Ok(())}.into();
177+
178+
o.set_high();
179+
assert_eq!(o.inner().state, true);
180+
181+
o.set_low();
182+
assert_eq!(o.inner().state, false);
183+
}
184+
185+
#[test]
186+
#[should_panic]
187+
fn v1_v2_output_panic() {
188+
let mut o: OldOutputPin<_> = NewOutputPinImpl{state: false, res: Err(())}.into();
189+
190+
o.set_high();
191+
}
192+
160193
#[cfg(feature = "unproven")]
161194
struct NewInputPinImpl {
162-
state: bool,
195+
state: Result<bool, ()>,
163196
}
164197

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

169202
fn is_low(&self) -> Result<bool, Self::Error> {
170-
Ok(!self.state)
203+
self.state.map(|v| v == false)
171204
}
172205
fn is_high(&self) -> Result<bool, Self::Error>{
173-
Ok(self.state)
206+
self.state.map(|v| v == true)
174207
}
175208
}
176209

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

233+
#[cfg(feature = "unproven")]
234+
#[test]
235+
fn v1_v2_input_state() {
236+
let i: OldInputPin<_> = NewInputPinImpl{state: Ok(false)}.into();
237+
238+
assert_eq!(i.is_low(), true);
239+
assert_eq!(i.is_high(), false);
240+
}
241+
242+
#[cfg(feature = "unproven")]
243+
#[test]
244+
#[should_panic]
245+
fn v1_v2_input_panic() {
246+
let i: OldInputPin<_> = NewInputPinImpl{state: Err(())}.into();
247+
248+
i.is_low();
249+
}
250+
200251
}

src/digital/v2_compat.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ impl <T> v2::OutputPin for T
1111
where
1212
T: v1::OutputPin,
1313
{
14+
// TODO: update to ! when never_type is stabilized
1415
type Error = ();
1516

1617
/// Toggle pin output
@@ -46,6 +47,7 @@ impl <T> v2::InputPin for T
4647
where
4748
T: v1::InputPin
4849
{
50+
// TODO: update to ! when never_type is stabilized
4951
type Error = ();
5052

5153
/// Toggle pin output
@@ -97,6 +99,17 @@ mod tests {
9799
let _c = NewOutputPinConsumer::new(i);
98100
}
99101

102+
#[test]
103+
fn v2_v1_output_state() {
104+
let mut o = OldOutputPinImpl{state: false};
105+
106+
v2::OutputPin::set_high(&mut o).unwrap();
107+
assert_eq!(o.state, true);
108+
109+
v2::OutputPin::set_low(&mut o).unwrap();
110+
assert_eq!(o.state, false);
111+
}
112+
100113
#[cfg(feature = "unproven")]
101114
#[allow(deprecated)]
102115
struct OldInputPinImpl {
@@ -130,8 +143,16 @@ mod tests {
130143
#[cfg(feature = "unproven")]
131144
#[test]
132145
fn v2_v1_input_implicit() {
133-
let i = OldOutputPinImpl{state: false};
134-
let _c = NewOutputPinConsumer::new(i);
146+
let i = OldInputPinImpl{state: false};
147+
let _c = NewInputPinConsumer::new(i);
135148
}
136149

150+
#[cfg(feature = "unproven")]
151+
#[test]
152+
fn v2_v1_input_state() {
153+
let mut i = OldInputPinImpl{state: false};
154+
155+
assert_eq!(v2::InputPin::is_high(&mut i).unwrap(), false);
156+
assert_eq!(v2::InputPin::is_low(&mut i).unwrap(), true);
157+
}
137158
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@
683683
//! ```
684684
685685
#![deny(missing_docs)]
686-
//#![deny(warnings)]
686+
#![deny(warnings)]
687687
#![no_std]
688688

689689
#[macro_use]

0 commit comments

Comments
 (0)