Skip to content

Commit 20fad3e

Browse files
bors[bot]little-arhatryankurteeldruin
authored
Merge #220
220: [0.2.x] Fixes to digital compatibility shim r=ryankurte a=eldruin I cherry-picked #138 and some improvements to docs and included #199. Co-authored-by: Roma Sokolov <[email protected]> Co-authored-by: Ryan Kurte <[email protected]> Co-authored-by: Diego Barrios Romero <[email protected]>
2 parents 4c07da5 + 55461c3 commit 20fad3e

File tree

3 files changed

+114
-12
lines changed

3 files changed

+114
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1212

1313
### Changed
1414

15+
- Fix the input pin v2->v1 compatibility shim constructor, where `OldInputPin::new`
16+
was incorrectly implemented for `v1::OutputPin` values.
17+
1518

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

src/digital/v1_compat.rs

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,39 @@
1-
//! v1 compatibility wrapper
2-
//! this module adds reverse support for v2 digital traits
3-
//! v2 traits must be explicitly cast to the v1 version using `.into()`,
4-
//! and will panic on internal errors
1+
//! v1 compatibility wrappers
2+
//!
3+
//! This module provides wrappers to support use of v2 implementations with
4+
//! v1 consumers. v2 traits must be explicitly cast to the v1 version using
5+
//! `.into()`, and will panic on internal errors
6+
//!
7+
//! ```
8+
//! extern crate embedded_hal;
9+
//! use embedded_hal::digital::{v1, v2, v1_compat::OldOutputPin};
10+
//!
11+
//! struct NewOutputPinImpl {}
12+
//!
13+
//! impl v2::OutputPin for NewOutputPinImpl {
14+
//! type Error = ();
15+
//! fn set_low(&mut self) -> Result<(), Self::Error> { Ok(()) }
16+
//! fn set_high(&mut self) -> Result<(), Self::Error>{ Ok(()) }
17+
//! }
18+
//!
19+
//! struct OldOutputPinConsumer<T: v1::OutputPin> {
20+
//! _pin: T,
21+
//! }
22+
//!
23+
//! impl <T>OldOutputPinConsumer<T>
24+
//! where T: v1::OutputPin {
25+
//! pub fn new(pin: T) -> OldOutputPinConsumer<T> {
26+
//! OldOutputPinConsumer{ _pin: pin }
27+
//! }
28+
//! }
29+
//!
30+
//! fn main() {
31+
//! let pin = NewOutputPinImpl{};
32+
//! let _consumer: OldOutputPinConsumer<OldOutputPin<_>> = OldOutputPinConsumer::new(pin.into());
33+
//! }
34+
//! ```
35+
//!
36+
537

638
#[allow(deprecated)]
739
use super::v1;
@@ -60,7 +92,7 @@ where
6092
/// where errors will panic.
6193
#[cfg(feature = "unproven")]
6294
#[allow(deprecated)]
63-
impl <T, E> v1::StatefulOutputPin for OldOutputPin<T>
95+
impl <T, E> v1::StatefulOutputPin for OldOutputPin<T>
6496
where
6597
T: v2::StatefulOutputPin<Error=E>,
6698
E: core::fmt::Debug,
@@ -84,7 +116,7 @@ pub struct OldInputPin<T> {
84116
#[cfg(feature = "unproven")]
85117
impl <T, E> OldInputPin<T>
86118
where
87-
T: v2::OutputPin<Error=E>,
119+
T: v2::InputPin<Error=E>,
88120
E: core::fmt::Debug,
89121
{
90122
/// Create an `OldInputPin` wrapper around a `v2::InputPin`.
@@ -159,8 +191,8 @@ mod tests {
159191
}
160192

161193
#[allow(deprecated)]
162-
impl <T>OldOutputPinConsumer<T>
163-
where T: v1::OutputPin
194+
impl <T>OldOutputPinConsumer<T>
195+
where T: v1::OutputPin
164196
{
165197
pub fn new(pin: T) -> OldOutputPinConsumer<T> {
166198
OldOutputPinConsumer{ _pin: pin }
@@ -181,7 +213,7 @@ mod tests {
181213
assert_eq!(o.inner().state, true);
182214

183215
o.set_low();
184-
assert_eq!(o.inner().state, false);
216+
assert_eq!(o.inner().state, false);
185217
}
186218

187219
#[test]
@@ -220,8 +252,8 @@ mod tests {
220252

221253
#[cfg(feature = "unproven")]
222254
#[allow(deprecated)]
223-
impl <T>OldInputPinConsumer<T>
224-
where T: v1::InputPin
255+
impl <T>OldInputPinConsumer<T>
256+
where T: v1::InputPin
225257
{
226258
pub fn new(pin: T) -> OldInputPinConsumer<T> {
227259
OldInputPinConsumer{ _pin: pin }

src/digital/v2_compat.rs

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,36 @@
11
//! v2 compatibility shims
2-
//! this module adds implicit forward support to v1 digital traits
2+
//!
3+
//! This module adds implicit forward support to v1 digital traits,
4+
//! allowing v1 implementations to be directly used with v2 consumers.
5+
//!
6+
//! ```
7+
//! extern crate embedded_hal;
8+
//! use embedded_hal::digital::{v1, v2};
9+
//!
10+
//! struct OldOutputPinImpl { }
11+
//!
12+
//! impl v1::OutputPin for OldOutputPinImpl {
13+
//! fn set_low(&mut self) { }
14+
//! fn set_high(&mut self) { }
15+
//! }
16+
//!
17+
//! struct NewOutputPinConsumer<T: v2::OutputPin> {
18+
//! _pin: T,
19+
//! }
20+
//!
21+
//! impl <T>NewOutputPinConsumer<T>
22+
//! where T: v2::OutputPin {
23+
//! pub fn new(pin: T) -> NewOutputPinConsumer<T> {
24+
//! NewOutputPinConsumer{ _pin: pin }
25+
//! }
26+
//! }
27+
//!
28+
//! fn main() {
29+
//! let pin = OldOutputPinImpl{};
30+
//! let _consumer = NewOutputPinConsumer::new(pin);
31+
//! }
32+
//! ```
33+
//!
334
435
#[allow(deprecated)]
536
use super::v1;
@@ -39,6 +70,9 @@ where
3970
}
4071
}
4172

73+
#[cfg(feature = "unproven")]
74+
#[allow(deprecated)]
75+
impl<T> v2::toggleable::Default for T where T: v1::toggleable::Default {}
4276

4377
/// Implementation of fallible `v2::InputPin` for `v1::InputPin` digital traits
4478
#[cfg(feature = "unproven")]
@@ -81,6 +115,20 @@ mod tests {
81115
}
82116
}
83117

118+
#[allow(deprecated)]
119+
impl v1::StatefulOutputPin for OldOutputPinImpl {
120+
fn is_set_low(&self) -> bool {
121+
self.state == false
122+
}
123+
124+
fn is_set_high(&self) -> bool {
125+
self.state == true
126+
}
127+
}
128+
129+
#[allow(deprecated)]
130+
impl v1::toggleable::Default for OldOutputPinImpl {}
131+
84132
struct NewOutputPinConsumer<T: v2::OutputPin> {
85133
_pin: T,
86134
}
@@ -92,6 +140,25 @@ mod tests {
92140
}
93141
}
94142

143+
struct NewToggleablePinConsumer<T: v2::ToggleableOutputPin> {
144+
_pin: T,
145+
}
146+
147+
impl<T> NewToggleablePinConsumer<T>
148+
where
149+
T: v2::ToggleableOutputPin,
150+
{
151+
pub fn new(pin: T) -> NewToggleablePinConsumer<T> {
152+
NewToggleablePinConsumer { _pin: pin }
153+
}
154+
}
155+
156+
#[test]
157+
fn v2_v1_toggleable_implicit() {
158+
let i = OldOutputPinImpl { state: false };
159+
let _c = NewToggleablePinConsumer::new(i);
160+
}
161+
95162
#[test]
96163
fn v2_v1_output_implicit() {
97164
let i = OldOutputPinImpl{state: false};

0 commit comments

Comments
 (0)