1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//! v1 compatibility wrappers
//!
//! This module provides wrappers to support use of v2 implementations with
//! v1 consumers. v2 traits must be explicitly cast to the v1 version using
//! `.into()`, and will panic on internal errors
//!
//! ```
//! extern crate embedded_hal;
//! use embedded_hal::digital::{v1, v2, v1_compat::OldOutputPin};
//!
//! struct NewOutputPinImpl {}
//!
//! impl v2::OutputPin for NewOutputPinImpl {
//!     type Error = ();
//!     fn set_low(&mut self) -> Result<(), Self::Error> { Ok(()) }
//!     fn set_high(&mut self) -> Result<(), Self::Error>{ Ok(()) }
//! }
//!
//! struct OldOutputPinConsumer<T: v1::OutputPin> {
//!     _pin: T,
//! }
//!
//! impl <T>OldOutputPinConsumer<T>
//! where T: v1::OutputPin {
//!     pub fn new(pin: T) -> OldOutputPinConsumer<T> {
//!         OldOutputPinConsumer{ _pin: pin }
//!     }
//! }
//!
//! fn main() {
//!     let pin = NewOutputPinImpl{};
//!     let _consumer: OldOutputPinConsumer<OldOutputPin<_>> = OldOutputPinConsumer::new(pin.into());
//! }
//! ```
//!

#[allow(deprecated)]
use super::v1;
use super::v2;

/// Wrapper to allow fallible `v2::OutputPin` traits to be converted to `v1::OutputPin` traits
pub struct OldOutputPin<T> {
    pin: T,
}

impl<T, E> OldOutputPin<T>
where
    T: v2::OutputPin<Error = E>,
    E: core::fmt::Debug,
{
    /// Create a new OldOutputPin wrapper around a `v2::OutputPin`
    pub fn new(pin: T) -> Self {
        Self { pin }
    }

    /// Fetch a reference to the inner `v2::OutputPin` impl
    #[cfg(test)]
    fn inner(&self) -> &T {
        &self.pin
    }
}

impl<T, E> From<T> for OldOutputPin<T>
where
    T: v2::OutputPin<Error = E>,
    E: core::fmt::Debug,
{
    fn from(pin: T) -> Self {
        OldOutputPin { pin }
    }
}

/// Implementation of `v1::OutputPin` trait for fallible `v2::OutputPin` output pins
/// where errors will panic.
#[allow(deprecated)]
impl<T, E> v1::OutputPin for OldOutputPin<T>
where
    T: v2::OutputPin<Error = E>,
    E: core::fmt::Debug,
{
    fn set_low(&mut self) {
        self.pin.set_low().unwrap()
    }

    fn set_high(&mut self) {
        self.pin.set_high().unwrap()
    }
}

/// Implementation of `v1::StatefulOutputPin` trait for `v2::StatefulOutputPin` fallible pins
/// where errors will panic.
#[cfg(feature = "unproven")]
#[allow(deprecated)]
impl<T, E> v1::StatefulOutputPin for OldOutputPin<T>
where
    T: v2::StatefulOutputPin<Error = E>,
    E: core::fmt::Debug,
{
    fn is_set_low(&self) -> bool {
        self.pin.is_set_low().unwrap()
    }

    fn is_set_high(&self) -> bool {
        self.pin.is_set_high().unwrap()
    }
}

/// Wrapper to allow fallible `v2::InputPin` traits to be converted to `v1::InputPin` traits
/// where errors will panic.
#[cfg(feature = "unproven")]
pub struct OldInputPin<T> {
    pin: T,
}

#[cfg(feature = "unproven")]
impl<T, E> OldInputPin<T>
where
    T: v2::InputPin<Error = E>,
    E: core::fmt::Debug,
{
    /// Create an `OldInputPin` wrapper around a `v2::InputPin`.
    pub fn new(pin: T) -> Self {
        Self { pin }
    }
}

#[cfg(feature = "unproven")]
impl<T, E> From<T> for OldInputPin<T>
where
    T: v2::InputPin<Error = E>,
    E: core::fmt::Debug,
{
    fn from(pin: T) -> Self {
        OldInputPin { pin }
    }
}

/// Implementation of `v1::InputPin` trait for `v2::InputPin` fallible pins
/// where errors will panic.
#[cfg(feature = "unproven")]
#[allow(deprecated)]
impl<T, E> v1::InputPin for OldInputPin<T>
where
    T: v2::InputPin<Error = E>,
    E: core::fmt::Debug,
{
    fn is_low(&self) -> bool {
        self.pin.is_low().unwrap()
    }

    fn is_high(&self) -> bool {
        self.pin.is_high().unwrap()
    }
}

#[cfg(test)]
#[allow(deprecated)]
mod tests {
    use super::*;

    #[allow(deprecated)]
    use crate::digital::v1;
    use crate::digital::v2;

    use crate::digital::v1::OutputPin;

    #[derive(Clone)]
    struct NewOutputPinImpl {
        state: bool,
        res: Result<(), ()>,
    }

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

        fn set_low(&mut self) -> Result<(), Self::Error> {
            self.state = false;
            self.res
        }
        fn set_high(&mut self) -> Result<(), Self::Error> {
            self.state = true;
            self.res
        }
    }

    #[allow(deprecated)]
    struct OldOutputPinConsumer<T: v1::OutputPin> {
        _pin: T,
    }

    #[allow(deprecated)]
    impl<T> OldOutputPinConsumer<T>
    where
        T: v1::OutputPin,
    {
        pub fn new(pin: T) -> OldOutputPinConsumer<T> {
            OldOutputPinConsumer { _pin: pin }
        }
    }

    #[test]
    fn v1_v2_output_explicit() {
        let i = NewOutputPinImpl {
            state: false,
            res: Ok(()),
        };
        let _c: OldOutputPinConsumer<OldOutputPin<_>> = OldOutputPinConsumer::new(i.into());
    }

    #[test]
    fn v1_v2_output_state() {
        let mut o: OldOutputPin<_> = NewOutputPinImpl {
            state: false,
            res: Ok(()),
        }
        .into();

        o.set_high();
        assert_eq!(o.inner().state, true);

        o.set_low();
        assert_eq!(o.inner().state, false);
    }

    #[test]
    #[should_panic]
    fn v1_v2_output_panic() {
        let mut o: OldOutputPin<_> = NewOutputPinImpl {
            state: false,
            res: Err(()),
        }
        .into();

        o.set_high();
    }

    #[cfg(feature = "unproven")]
    use crate::digital::v1::InputPin;

    #[cfg(feature = "unproven")]
    struct NewInputPinImpl {
        state: Result<bool, ()>,
    }

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

        fn is_low(&self) -> Result<bool, Self::Error> {
            self.state.map(|v| v == false)
        }
        fn is_high(&self) -> Result<bool, Self::Error> {
            self.state.map(|v| v == true)
        }
    }

    #[cfg(feature = "unproven")]
    #[allow(deprecated)]
    struct OldInputPinConsumer<T: v1::InputPin> {
        _pin: T,
    }

    #[cfg(feature = "unproven")]
    #[allow(deprecated)]
    impl<T> OldInputPinConsumer<T>
    where
        T: v1::InputPin,
    {
        pub fn new(pin: T) -> OldInputPinConsumer<T> {
            OldInputPinConsumer { _pin: pin }
        }
    }

    #[cfg(feature = "unproven")]
    #[test]
    fn v1_v2_input_explicit() {
        let i = NewInputPinImpl { state: Ok(false) };
        let _c: OldInputPinConsumer<OldInputPin<_>> = OldInputPinConsumer::new(i.into());
    }

    #[cfg(feature = "unproven")]
    #[test]
    fn v1_v2_input_state() {
        let i: OldInputPin<_> = NewInputPinImpl { state: Ok(false) }.into();

        assert_eq!(i.is_low(), true);
        assert_eq!(i.is_high(), false);
    }

    #[cfg(feature = "unproven")]
    #[test]
    #[should_panic]
    fn v1_v2_input_panic() {
        let i: OldInputPin<_> = NewInputPinImpl { state: Err(()) }.into();

        i.is_low();
    }
}