11//! v1 compatibility wrappers
2- //!
2+ //!
33//! 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
55//! `.into()`, and will panic on internal errors
6- //!
6+ //!
77//! ```
88//! extern crate embedded_hal;
99//! use embedded_hal::digital::{v1, v2, v1_compat::OldOutputPin};
10- //!
10+ //!
1111//! struct NewOutputPinImpl {}
12- //!
12+ //!
1313//! impl v2::OutputPin for NewOutputPinImpl {
1414//! type Error = ();
1515//! fn set_low(&mut self) -> Result<(), Self::Error> { Ok(()) }
1616//! fn set_high(&mut self) -> Result<(), Self::Error>{ Ok(()) }
1717//! }
18- //!
18+ //!
1919//! struct OldOutputPinConsumer<T: v1::OutputPin> {
2020//! _pin: T,
2121//! }
22- //!
23- //! impl <T>OldOutputPinConsumer<T>
22+ //!
23+ //! impl <T>OldOutputPinConsumer<T>
2424//! where T: v1::OutputPin {
2525//! pub fn new(pin: T) -> OldOutputPinConsumer<T> {
2626//! OldOutputPinConsumer{ _pin: pin }
2727//! }
2828//! }
29- //!
29+ //!
3030//! fn main() {
3131//! let pin = NewOutputPinImpl{};
3232//! let _consumer: OldOutputPinConsumer<OldOutputPin<_>> = OldOutputPinConsumer::new(pin.into());
3333//! }
3434//! ```
35- //!
36-
35+ //!
3736
3837#[ allow( deprecated) ]
3938use super :: v1;
@@ -44,14 +43,14 @@ pub struct OldOutputPin<T> {
4443 pin : T ,
4544}
4645
47- impl < T , E > OldOutputPin < T >
46+ impl < T , E > OldOutputPin < T >
4847where
49- T : v2:: OutputPin < Error = E > ,
48+ T : v2:: OutputPin < Error = E > ,
5049 E : core:: fmt:: Debug ,
5150{
5251 /// Create a new OldOutputPin wrapper around a `v2::OutputPin`
5352 pub fn new ( pin : T ) -> Self {
54- Self { pin}
53+ Self { pin }
5554 }
5655
5756 /// Fetch a reference to the inner `v2::OutputPin` impl
@@ -61,22 +60,22 @@ where
6160 }
6261}
6362
64- impl < T , E > From < T > for OldOutputPin < T >
63+ impl < T , E > From < T > for OldOutputPin < T >
6564where
66- T : v2:: OutputPin < Error = E > ,
65+ T : v2:: OutputPin < Error = E > ,
6766 E : core:: fmt:: Debug ,
6867{
6968 fn from ( pin : T ) -> Self {
70- OldOutputPin { pin}
69+ OldOutputPin { pin }
7170 }
7271}
7372
7473/// Implementation of `v1::OutputPin` trait for fallible `v2::OutputPin` output pins
7574/// where errors will panic.
7675#[ allow( deprecated) ]
77- impl < T , E > v1:: OutputPin for OldOutputPin < T >
76+ impl < T , E > v1:: OutputPin for OldOutputPin < T >
7877where
79- T : v2:: OutputPin < Error = E > ,
78+ T : v2:: OutputPin < Error = E > ,
8079 E : core:: fmt:: Debug ,
8180{
8281 fn set_low ( & mut self ) {
9291/// where errors will panic.
9392#[ cfg( feature = "unproven" ) ]
9493#[ allow( deprecated) ]
95- impl < T , E > v1:: StatefulOutputPin for OldOutputPin < T >
94+ impl < T , E > v1:: StatefulOutputPin for OldOutputPin < T >
9695where
97- T : v2:: StatefulOutputPin < Error = E > ,
96+ T : v2:: StatefulOutputPin < Error = E > ,
9897 E : core:: fmt:: Debug ,
9998{
10099 fn is_set_low ( & self ) -> bool {
@@ -112,34 +111,33 @@ pub struct OldInputPin<T> {
112111 pin : T ,
113112}
114113
115- impl < T , E > OldInputPin < T >
114+ impl < T , E > OldInputPin < T >
116115where
117- T : v2:: OutputPin < Error = E > ,
116+ T : v2:: OutputPin < Error = E > ,
118117 E : core:: fmt:: Debug ,
119118{
120119 /// Create an `OldInputPin` wrapper around a `v2::InputPin`.
121120 pub fn new ( pin : T ) -> Self {
122- Self { pin}
121+ Self { pin }
123122 }
124-
125123}
126124
127- impl < T , E > From < T > for OldInputPin < T >
125+ impl < T , E > From < T > for OldInputPin < T >
128126where
129- T : v2:: InputPin < Error = E > ,
127+ T : v2:: InputPin < Error = E > ,
130128 E : core:: fmt:: Debug ,
131129{
132130 fn from ( pin : T ) -> Self {
133- OldInputPin { pin}
131+ OldInputPin { pin }
134132 }
135133}
136134
137135/// Implementation of `v1::InputPin` trait for `v2::InputPin` fallible pins
138136/// where errors will panic.
139137#[ allow( deprecated) ]
140- impl < T , E > v1:: InputPin for OldInputPin < T >
138+ impl < T , E > v1:: InputPin for OldInputPin < T >
141139where
142- T : v2:: InputPin < Error = E > ,
140+ T : v2:: InputPin < Error = E > ,
143141 E : core:: fmt:: Debug ,
144142{
145143 fn is_low ( & self ) -> bool {
@@ -165,7 +163,7 @@ mod tests {
165163 #[ derive( Clone ) ]
166164 struct NewOutputPinImpl {
167165 state : bool ,
168- res : Result < ( ) , ( ) >
166+ res : Result < ( ) , ( ) > ,
169167 }
170168
171169 impl v2:: OutputPin for NewOutputPinImpl {
@@ -175,7 +173,7 @@ mod tests {
175173 self . state = false ;
176174 self . res
177175 }
178- fn set_high ( & mut self ) -> Result < ( ) , Self :: Error > {
176+ fn set_high ( & mut self ) -> Result < ( ) , Self :: Error > {
179177 self . state = true ;
180178 self . res
181179 }
@@ -187,35 +185,47 @@ mod tests {
187185 }
188186
189187 #[ allow( deprecated) ]
190- impl < T > OldOutputPinConsumer < T >
191- where T : v1:: OutputPin
188+ impl < T > OldOutputPinConsumer < T >
189+ where
190+ T : v1:: OutputPin ,
192191 {
193192 pub fn new ( pin : T ) -> OldOutputPinConsumer < T > {
194- OldOutputPinConsumer { _pin : pin }
193+ OldOutputPinConsumer { _pin : pin }
195194 }
196195 }
197196
198197 #[ test]
199198 fn v1_v2_output_explicit ( ) {
200- let i = NewOutputPinImpl { state : false , res : Ok ( ( ) ) } ;
199+ let i = NewOutputPinImpl {
200+ state : false ,
201+ res : Ok ( ( ) ) ,
202+ } ;
201203 let _c: OldOutputPinConsumer < OldOutputPin < _ > > = OldOutputPinConsumer :: new ( i. into ( ) ) ;
202204 }
203205
204206 #[ test]
205207 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 ( ) ;
207213
208214 o. set_high ( ) ;
209215 assert_eq ! ( o. inner( ) . state, true ) ;
210216
211217 o. set_low ( ) ;
212- assert_eq ! ( o. inner( ) . state, false ) ;
218+ assert_eq ! ( o. inner( ) . state, false ) ;
213219 }
214220
215221 #[ test]
216222 #[ should_panic]
217223 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 ( ) ;
219229
220230 o. set_high ( ) ;
221231 }
@@ -232,7 +242,7 @@ mod tests {
232242 fn is_low ( & self ) -> Result < bool , Self :: Error > {
233243 self . state . map ( |v| v == false )
234244 }
235- fn is_high ( & self ) -> Result < bool , Self :: Error > {
245+ fn is_high ( & self ) -> Result < bool , Self :: Error > {
236246 self . state . map ( |v| v == true )
237247 }
238248 }
@@ -243,23 +253,24 @@ mod tests {
243253 }
244254
245255 #[ allow( deprecated) ]
246- impl < T > OldInputPinConsumer < T >
247- where T : v1:: InputPin
256+ impl < T > OldInputPinConsumer < T >
257+ where
258+ T : v1:: InputPin ,
248259 {
249260 pub fn new ( pin : T ) -> OldInputPinConsumer < T > {
250- OldInputPinConsumer { _pin : pin }
261+ OldInputPinConsumer { _pin : pin }
251262 }
252263 }
253264
254265 #[ test]
255266 fn v1_v2_input_explicit ( ) {
256- let i = NewInputPinImpl { state : Ok ( false ) } ;
267+ let i = NewInputPinImpl { state : Ok ( false ) } ;
257268 let _c: OldInputPinConsumer < OldInputPin < _ > > = OldInputPinConsumer :: new ( i. into ( ) ) ;
258269 }
259270
260271 #[ test]
261272 fn v1_v2_input_state ( ) {
262- let i: OldInputPin < _ > = NewInputPinImpl { state : Ok ( false ) } . into ( ) ;
273+ let i: OldInputPin < _ > = NewInputPinImpl { state : Ok ( false ) } . into ( ) ;
263274
264275 assert_eq ! ( i. is_low( ) , true ) ;
265276 assert_eq ! ( i. is_high( ) , false ) ;
@@ -268,9 +279,8 @@ mod tests {
268279 #[ test]
269280 #[ should_panic]
270281 fn v1_v2_input_panic ( ) {
271- let i: OldInputPin < _ > = NewInputPinImpl { state : Err ( ( ) ) } . into ( ) ;
282+ let i: OldInputPin < _ > = NewInputPinImpl { state : Err ( ( ) ) } . into ( ) ;
272283
273284 i. is_low ( ) ;
274285 }
275-
276286}
0 commit comments