1
- //! v1 compatibility shims
1
+ //! v1 compatibility wrapper
2
2
//! this module adds reverse support for v2 digital traits
3
- //! only on of v1_compat or v2_compat may be included at a given time
3
+ //! v2 traits must be explicitly cast to the v1 version using `.into()`.
4
4
5
5
#[ allow( deprecated) ]
6
6
use super :: v1;
7
7
use super :: v2;
8
8
9
+ /// Wrapper to allow v2 fallible OutputPin traits to be converted to v1 traits
10
+ pub struct OldOutputPin < T > {
11
+ pin : T ,
12
+ }
13
+
14
+ impl < T , ERR > From < T > for OldOutputPin < T >
15
+ where
16
+ T : v2:: OutputPin < Error =ERR > ,
17
+ ERR : core:: fmt:: Debug ,
18
+ {
19
+ fn from ( pin : T ) -> Self {
20
+ OldOutputPin { pin}
21
+ }
22
+ }
23
+
9
24
/// Implementation of v1 OutputPin trait for v2 fallible output pins
10
25
#[ allow( deprecated) ]
11
- impl < T , ERR > v1:: OutputPin for T
26
+ impl < T , ERR > v1:: OutputPin for OldOutputPin < T >
12
27
where
13
28
T : v2:: OutputPin < Error =ERR > ,
14
29
ERR : core:: fmt:: Debug ,
15
30
{
16
31
fn set_low ( & mut self ) {
17
- self . set_low ( ) . unwrap ( )
32
+ self . pin . set_low ( ) . unwrap ( )
18
33
}
19
34
20
35
fn set_high ( & mut self ) {
21
- self . set_high ( ) . unwrap ( )
36
+ self . pin . set_high ( ) . unwrap ( )
22
37
}
23
38
}
24
39
25
40
/// Implementation of v1 StatefulOutputPin trait for v2 fallible pins
26
41
#[ cfg( feature = "unproven" ) ]
27
42
#[ allow( deprecated) ]
28
- impl < T , ERR > v1:: StatefulOutputPin for T
43
+ impl < T , ERR > v1:: StatefulOutputPin for OldOutputPin < T >
29
44
where
30
45
T : v2:: StatefulOutputPin < Error =ERR > ,
31
46
ERR : core:: fmt:: Debug ,
32
47
{
33
48
fn is_set_low ( & self ) -> bool {
34
- self . is_set_low ( ) . unwrap ( )
49
+ self . pin . is_set_low ( ) . unwrap ( )
35
50
}
36
51
37
52
fn is_set_high ( & self ) -> bool {
38
- self . is_set_high ( ) . unwrap ( )
53
+ self . pin . is_set_high ( ) . unwrap ( )
54
+ }
55
+ }
56
+
57
+ /// Wrapper to allow v2 fallible InputPin traits to be converted to v1 traits
58
+ #[ cfg( feature = "unproven" ) ]
59
+ pub struct OldInputPin < T > {
60
+ pin : T ,
61
+ }
62
+
63
+ #[ cfg( feature = "unproven" ) ]
64
+ impl < T , ERR > From < T > for OldInputPin < T >
65
+ where
66
+ T : v2:: InputPin < Error =ERR > ,
67
+ ERR : core:: fmt:: Debug ,
68
+ {
69
+ fn from ( pin : T ) -> Self {
70
+ OldInputPin { pin}
39
71
}
40
72
}
41
73
42
74
/// Implementation of v0.2 InputPin trait for v0.3 fallible pins
43
75
#[ cfg( feature = "unproven" ) ]
44
76
#[ allow( deprecated) ]
45
- impl < T , ERR > v1:: InputPin for T
77
+ impl < T , ERR > v1:: InputPin for OldInputPin < T >
46
78
where
47
79
T : v2:: InputPin < Error =ERR > ,
48
80
ERR : core:: fmt:: Debug ,
49
81
{
50
82
fn is_low ( & self ) -> bool {
51
- self . is_low ( ) . unwrap ( )
83
+ self . pin . is_low ( ) . unwrap ( )
52
84
}
53
85
54
86
fn is_high ( & self ) -> bool {
55
- self . is_high ( ) . unwrap ( )
87
+ self . pin . is_high ( ) . unwrap ( )
56
88
}
57
89
}
58
90
59
91
#[ cfg( test) ]
60
92
#[ allow( deprecated) ]
61
93
mod tests {
94
+ use super :: * ;
95
+
62
96
#[ allow( deprecated) ]
63
97
use crate :: digital:: v1;
64
98
use crate :: digital:: v2;
@@ -87,16 +121,57 @@ mod tests {
87
121
88
122
#[ allow( deprecated) ]
89
123
impl < T > OldOutputPinConsumer < T >
90
- where T : v1:: OutputPin {
124
+ where T : v1:: OutputPin
125
+ {
91
126
pub fn new ( pin : T ) -> OldOutputPinConsumer < T > {
92
127
OldOutputPinConsumer { _pin : pin }
93
128
}
94
129
}
95
130
96
131
#[ test]
97
- fn new_old ( ) {
132
+ fn v1_v2_output_explicit ( ) {
98
133
let i = NewOutputPinImpl { state : false } ;
99
- let _c = OldOutputPinConsumer :: new ( i) ;
134
+ let _c: OldOutputPinConsumer < OldOutputPin < _ > > = OldOutputPinConsumer :: new ( i. into ( ) ) ;
135
+ }
136
+
137
+ #[ cfg( feature = "unproven" ) ]
138
+ struct NewInputPinImpl {
139
+ state : bool ,
140
+ }
141
+
142
+ #[ cfg( feature = "unproven" ) ]
143
+ impl v2:: InputPin for NewInputPinImpl {
144
+ type Error = ( ) ;
145
+
146
+ fn is_low ( & self ) -> Result < bool , Self :: Error > {
147
+ Ok ( !self . state )
148
+ }
149
+ fn is_high ( & self ) -> Result < bool , Self :: Error > {
150
+ Ok ( self . state )
151
+ }
152
+ }
153
+
154
+ #[ cfg( feature = "unproven" ) ]
155
+ #[ allow( deprecated) ]
156
+ struct OldInputPinConsumer < T : v1:: InputPin > {
157
+ _pin : T ,
158
+ }
159
+
160
+ #[ cfg( feature = "unproven" ) ]
161
+ #[ allow( deprecated) ]
162
+ impl < T > OldInputPinConsumer < T >
163
+ where T : v1:: InputPin
164
+ {
165
+ pub fn new ( pin : T ) -> OldInputPinConsumer < T > {
166
+ OldInputPinConsumer { _pin : pin }
167
+ }
168
+ }
169
+
170
+ #[ cfg( feature = "unproven" ) ]
171
+ #[ test]
172
+ fn v1_v2_input_explicit ( ) {
173
+ let i = NewInputPinImpl { state : false } ;
174
+ let _c: OldInputPinConsumer < OldInputPin < _ > > = OldInputPinConsumer :: new ( i. into ( ) ) ;
100
175
}
101
176
102
177
}
0 commit comments