1
1
//! v1 compatibility wrappers
2
- //!
2
+ //!
3
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
4
+ //! v1 consumers. v2 traits must be explicitly cast to the v1 version using
5
5
//! `.into()`, and will panic on internal errors
6
- //!
6
+ //!
7
7
//! ```
8
8
//! extern crate embedded_hal;
9
9
//! use embedded_hal::digital::{v1, v2, v1_compat::OldOutputPin};
10
- //!
10
+ //!
11
11
//! struct NewOutputPinImpl {}
12
- //!
12
+ //!
13
13
//! impl v2::OutputPin for NewOutputPinImpl {
14
14
//! type Error = ();
15
15
//! fn set_low(&mut self) -> Result<(), Self::Error> { Ok(()) }
16
16
//! fn set_high(&mut self) -> Result<(), Self::Error>{ Ok(()) }
17
17
//! }
18
- //!
18
+ //!
19
19
//! struct OldOutputPinConsumer<T: v1::OutputPin> {
20
20
//! _pin: T,
21
21
//! }
22
- //!
23
- //! impl <T>OldOutputPinConsumer<T>
22
+ //!
23
+ //! impl <T>OldOutputPinConsumer<T>
24
24
//! where T: v1::OutputPin {
25
25
//! pub fn new(pin: T) -> OldOutputPinConsumer<T> {
26
26
//! OldOutputPinConsumer{ _pin: pin }
27
27
//! }
28
28
//! }
29
- //!
29
+ //!
30
30
//! fn main() {
31
31
//! let pin = NewOutputPinImpl{};
32
32
//! let _consumer: OldOutputPinConsumer<OldOutputPin<_>> = OldOutputPinConsumer::new(pin.into());
33
33
//! }
34
34
//! ```
35
- //!
36
-
35
+ //!
37
36
38
37
#[ allow( deprecated) ]
39
38
use super :: v1;
@@ -44,14 +43,14 @@ pub struct OldOutputPin<T> {
44
43
pin : T ,
45
44
}
46
45
47
- impl < T , E > OldOutputPin < T >
46
+ impl < T , E > OldOutputPin < T >
48
47
where
49
- T : v2:: OutputPin < Error = E > ,
48
+ T : v2:: OutputPin < Error = E > ,
50
49
E : core:: fmt:: Debug ,
51
50
{
52
51
/// Create a new OldOutputPin wrapper around a `v2::OutputPin`
53
52
pub fn new ( pin : T ) -> Self {
54
- Self { pin}
53
+ Self { pin }
55
54
}
56
55
57
56
/// Fetch a reference to the inner `v2::OutputPin` impl
@@ -61,22 +60,22 @@ where
61
60
}
62
61
}
63
62
64
- impl < T , E > From < T > for OldOutputPin < T >
63
+ impl < T , E > From < T > for OldOutputPin < T >
65
64
where
66
- T : v2:: OutputPin < Error = E > ,
65
+ T : v2:: OutputPin < Error = E > ,
67
66
E : core:: fmt:: Debug ,
68
67
{
69
68
fn from ( pin : T ) -> Self {
70
- OldOutputPin { pin}
69
+ OldOutputPin { pin }
71
70
}
72
71
}
73
72
74
73
/// Implementation of `v1::OutputPin` trait for fallible `v2::OutputPin` output pins
75
74
/// where errors will panic.
76
75
#[ allow( deprecated) ]
77
- impl < T , E > v1:: OutputPin for OldOutputPin < T >
76
+ impl < T , E > v1:: OutputPin for OldOutputPin < T >
78
77
where
79
- T : v2:: OutputPin < Error = E > ,
78
+ T : v2:: OutputPin < Error = E > ,
80
79
E : core:: fmt:: Debug ,
81
80
{
82
81
fn set_low ( & mut self ) {
92
91
/// where errors will panic.
93
92
#[ cfg( feature = "unproven" ) ]
94
93
#[ allow( deprecated) ]
95
- impl < T , E > v1:: StatefulOutputPin for OldOutputPin < T >
94
+ impl < T , E > v1:: StatefulOutputPin for OldOutputPin < T >
96
95
where
97
- T : v2:: StatefulOutputPin < Error = E > ,
96
+ T : v2:: StatefulOutputPin < Error = E > ,
98
97
E : core:: fmt:: Debug ,
99
98
{
100
99
fn is_set_low ( & self ) -> bool {
@@ -112,34 +111,33 @@ pub struct OldInputPin<T> {
112
111
pin : T ,
113
112
}
114
113
115
- impl < T , E > OldInputPin < T >
114
+ impl < T , E > OldInputPin < T >
116
115
where
117
- T : v2:: OutputPin < Error = E > ,
116
+ T : v2:: OutputPin < Error = E > ,
118
117
E : core:: fmt:: Debug ,
119
118
{
120
119
/// Create an `OldInputPin` wrapper around a `v2::InputPin`.
121
120
pub fn new ( pin : T ) -> Self {
122
- Self { pin}
121
+ Self { pin }
123
122
}
124
-
125
123
}
126
124
127
- impl < T , E > From < T > for OldInputPin < T >
125
+ impl < T , E > From < T > for OldInputPin < T >
128
126
where
129
- T : v2:: InputPin < Error = E > ,
127
+ T : v2:: InputPin < Error = E > ,
130
128
E : core:: fmt:: Debug ,
131
129
{
132
130
fn from ( pin : T ) -> Self {
133
- OldInputPin { pin}
131
+ OldInputPin { pin }
134
132
}
135
133
}
136
134
137
135
/// Implementation of `v1::InputPin` trait for `v2::InputPin` fallible pins
138
136
/// where errors will panic.
139
137
#[ allow( deprecated) ]
140
- impl < T , E > v1:: InputPin for OldInputPin < T >
138
+ impl < T , E > v1:: InputPin for OldInputPin < T >
141
139
where
142
- T : v2:: InputPin < Error = E > ,
140
+ T : v2:: InputPin < Error = E > ,
143
141
E : core:: fmt:: Debug ,
144
142
{
145
143
fn is_low ( & self ) -> bool {
@@ -165,7 +163,7 @@ mod tests {
165
163
#[ derive( Clone ) ]
166
164
struct NewOutputPinImpl {
167
165
state : bool ,
168
- res : Result < ( ) , ( ) >
166
+ res : Result < ( ) , ( ) > ,
169
167
}
170
168
171
169
impl v2:: OutputPin for NewOutputPinImpl {
@@ -175,7 +173,7 @@ mod tests {
175
173
self . state = false ;
176
174
self . res
177
175
}
178
- fn set_high ( & mut self ) -> Result < ( ) , Self :: Error > {
176
+ fn set_high ( & mut self ) -> Result < ( ) , Self :: Error > {
179
177
self . state = true ;
180
178
self . res
181
179
}
@@ -187,35 +185,47 @@ mod tests {
187
185
}
188
186
189
187
#[ allow( deprecated) ]
190
- impl < T > OldOutputPinConsumer < T >
191
- where T : v1:: OutputPin
188
+ impl < T > OldOutputPinConsumer < T >
189
+ where
190
+ T : v1:: OutputPin ,
192
191
{
193
192
pub fn new ( pin : T ) -> OldOutputPinConsumer < T > {
194
- OldOutputPinConsumer { _pin : pin }
193
+ OldOutputPinConsumer { _pin : pin }
195
194
}
196
195
}
197
196
198
197
#[ test]
199
198
fn v1_v2_output_explicit ( ) {
200
- let i = NewOutputPinImpl { state : false , res : Ok ( ( ) ) } ;
199
+ let i = NewOutputPinImpl {
200
+ state : false ,
201
+ res : Ok ( ( ) ) ,
202
+ } ;
201
203
let _c: OldOutputPinConsumer < OldOutputPin < _ > > = OldOutputPinConsumer :: new ( i. into ( ) ) ;
202
204
}
203
205
204
206
#[ test]
205
207
fn v1_v2_output_state ( ) {
206
- let mut o: OldOutputPin < _ > = NewOutputPinImpl { state : false , res : Ok ( ( ) ) } . into ( ) ;
208
+ let mut o: OldOutputPin < _ > = NewOutputPinImpl {
209
+ state : false ,
210
+ res : Ok ( ( ) ) ,
211
+ }
212
+ . into ( ) ;
207
213
208
214
o. set_high ( ) ;
209
215
assert_eq ! ( o. inner( ) . state, true ) ;
210
216
211
217
o. set_low ( ) ;
212
- assert_eq ! ( o. inner( ) . state, false ) ;
218
+ assert_eq ! ( o. inner( ) . state, false ) ;
213
219
}
214
220
215
221
#[ test]
216
222
#[ should_panic]
217
223
fn v1_v2_output_panic ( ) {
218
- let mut o: OldOutputPin < _ > = NewOutputPinImpl { state : false , res : Err ( ( ) ) } . into ( ) ;
224
+ let mut o: OldOutputPin < _ > = NewOutputPinImpl {
225
+ state : false ,
226
+ res : Err ( ( ) ) ,
227
+ }
228
+ . into ( ) ;
219
229
220
230
o. set_high ( ) ;
221
231
}
@@ -232,7 +242,7 @@ mod tests {
232
242
fn is_low ( & self ) -> Result < bool , Self :: Error > {
233
243
self . state . map ( |v| v == false )
234
244
}
235
- fn is_high ( & self ) -> Result < bool , Self :: Error > {
245
+ fn is_high ( & self ) -> Result < bool , Self :: Error > {
236
246
self . state . map ( |v| v == true )
237
247
}
238
248
}
@@ -243,23 +253,24 @@ mod tests {
243
253
}
244
254
245
255
#[ allow( deprecated) ]
246
- impl < T > OldInputPinConsumer < T >
247
- where T : v1:: InputPin
256
+ impl < T > OldInputPinConsumer < T >
257
+ where
258
+ T : v1:: InputPin ,
248
259
{
249
260
pub fn new ( pin : T ) -> OldInputPinConsumer < T > {
250
- OldInputPinConsumer { _pin : pin }
261
+ OldInputPinConsumer { _pin : pin }
251
262
}
252
263
}
253
264
254
265
#[ test]
255
266
fn v1_v2_input_explicit ( ) {
256
- let i = NewInputPinImpl { state : Ok ( false ) } ;
267
+ let i = NewInputPinImpl { state : Ok ( false ) } ;
257
268
let _c: OldInputPinConsumer < OldInputPin < _ > > = OldInputPinConsumer :: new ( i. into ( ) ) ;
258
269
}
259
270
260
271
#[ test]
261
272
fn v1_v2_input_state ( ) {
262
- let i: OldInputPin < _ > = NewInputPinImpl { state : Ok ( false ) } . into ( ) ;
273
+ let i: OldInputPin < _ > = NewInputPinImpl { state : Ok ( false ) } . into ( ) ;
263
274
264
275
assert_eq ! ( i. is_low( ) , true ) ;
265
276
assert_eq ! ( i. is_high( ) , false ) ;
@@ -268,9 +279,8 @@ mod tests {
268
279
#[ test]
269
280
#[ should_panic]
270
281
fn v1_v2_input_panic ( ) {
271
- let i: OldInputPin < _ > = NewInputPinImpl { state : Err ( ( ) ) } . into ( ) ;
282
+ let i: OldInputPin < _ > = NewInputPinImpl { state : Err ( ( ) ) } . into ( ) ;
272
283
273
284
i. is_low ( ) ;
274
285
}
275
-
276
286
}
0 commit comments