| 1 | //! v2 compatibility shims |
| 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 | //! |
| 34 | |
| 35 | #[allow (deprecated)] |
| 36 | use super::v1; |
| 37 | use super::v2; |
| 38 | |
| 39 | /// Implementation of fallible `v2::OutputPin` for `v1::OutputPin` traits |
| 40 | #[allow (deprecated)] |
| 41 | impl<T> v2::OutputPin for T |
| 42 | where |
| 43 | T: v1::OutputPin, |
| 44 | { |
| 45 | // TODO: update to ! when never_type is stabilized |
| 46 | type Error = (); |
| 47 | |
| 48 | fn set_low(&mut self) -> Result<(), Self::Error> { |
| 49 | Ok(self.set_low()) |
| 50 | } |
| 51 | |
| 52 | fn set_high(&mut self) -> Result<(), Self::Error> { |
| 53 | Ok(self.set_high()) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /// Implementation of fallible `v2::StatefulOutputPin` for `v1::StatefulOutputPin` digital traits |
| 58 | #[cfg (feature = "unproven" )] |
| 59 | #[allow (deprecated)] |
| 60 | impl<T> v2::StatefulOutputPin for T |
| 61 | where |
| 62 | T: v1::StatefulOutputPin + v1::OutputPin, |
| 63 | { |
| 64 | fn is_set_low(&self) -> Result<bool, Self::Error> { |
| 65 | Ok(self.is_set_low()) |
| 66 | } |
| 67 | |
| 68 | fn is_set_high(&self) -> Result<bool, Self::Error> { |
| 69 | Ok(self.is_set_high()) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | #[cfg (feature = "unproven" )] |
| 74 | #[allow (deprecated)] |
| 75 | impl<T> v2::toggleable::Default for T where T: v1::toggleable::Default {} |
| 76 | |
| 77 | /// Implementation of fallible `v2::InputPin` for `v1::InputPin` digital traits |
| 78 | #[cfg (feature = "unproven" )] |
| 79 | #[allow (deprecated)] |
| 80 | impl<T> v2::InputPin for T |
| 81 | where |
| 82 | T: v1::InputPin, |
| 83 | { |
| 84 | // TODO: update to ! when never_type is stabilized |
| 85 | type Error = (); |
| 86 | |
| 87 | fn is_low(&self) -> Result<bool, Self::Error> { |
| 88 | Ok(self.is_low()) |
| 89 | } |
| 90 | |
| 91 | fn is_high(&self) -> Result<bool, Self::Error> { |
| 92 | Ok(self.is_high()) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | #[cfg (test)] |
| 97 | mod tests { |
| 98 | |
| 99 | #[allow (deprecated)] |
| 100 | use crate::digital::v1; |
| 101 | use crate::digital::v2; |
| 102 | |
| 103 | #[allow (deprecated)] |
| 104 | struct OldOutputPinImpl { |
| 105 | state: bool, |
| 106 | } |
| 107 | |
| 108 | #[allow (deprecated)] |
| 109 | impl v1::OutputPin for OldOutputPinImpl { |
| 110 | fn set_low(&mut self) { |
| 111 | self.state = false; |
| 112 | } |
| 113 | fn set_high(&mut self) { |
| 114 | self.state = true; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | #[allow (deprecated)] |
| 119 | #[cfg (feature = "unproven" )] |
| 120 | impl v1::StatefulOutputPin for OldOutputPinImpl { |
| 121 | fn is_set_low(&self) -> bool { |
| 122 | self.state == false |
| 123 | } |
| 124 | |
| 125 | fn is_set_high(&self) -> bool { |
| 126 | self.state == true |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | #[allow (deprecated)] |
| 131 | #[cfg (feature = "unproven" )] |
| 132 | impl v1::toggleable::Default for OldOutputPinImpl {} |
| 133 | |
| 134 | struct NewOutputPinConsumer<T: v2::OutputPin> { |
| 135 | _pin: T, |
| 136 | } |
| 137 | |
| 138 | impl<T> NewOutputPinConsumer<T> |
| 139 | where |
| 140 | T: v2::OutputPin, |
| 141 | { |
| 142 | pub fn new(pin: T) -> NewOutputPinConsumer<T> { |
| 143 | NewOutputPinConsumer { _pin: pin } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | #[cfg (feature = "unproven" )] |
| 148 | struct NewToggleablePinConsumer<T: v2::ToggleableOutputPin> { |
| 149 | _pin: T, |
| 150 | } |
| 151 | |
| 152 | #[cfg (feature = "unproven" )] |
| 153 | impl<T> NewToggleablePinConsumer<T> |
| 154 | where |
| 155 | T: v2::ToggleableOutputPin, |
| 156 | { |
| 157 | pub fn new(pin: T) -> NewToggleablePinConsumer<T> { |
| 158 | NewToggleablePinConsumer { _pin: pin } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | #[test ] |
| 163 | #[cfg (feature = "unproven" )] |
| 164 | fn v2_v1_toggleable_implicit() { |
| 165 | let i = OldOutputPinImpl { state: false }; |
| 166 | let _c = NewToggleablePinConsumer::new(i); |
| 167 | } |
| 168 | |
| 169 | #[test ] |
| 170 | fn v2_v1_output_implicit() { |
| 171 | let i = OldOutputPinImpl { state: false }; |
| 172 | let _c = NewOutputPinConsumer::new(i); |
| 173 | } |
| 174 | |
| 175 | #[test ] |
| 176 | fn v2_v1_output_state() { |
| 177 | let mut o = OldOutputPinImpl { state: false }; |
| 178 | |
| 179 | v2::OutputPin::set_high(&mut o).unwrap(); |
| 180 | assert_eq!(o.state, true); |
| 181 | |
| 182 | v2::OutputPin::set_low(&mut o).unwrap(); |
| 183 | assert_eq!(o.state, false); |
| 184 | } |
| 185 | |
| 186 | #[cfg (feature = "unproven" )] |
| 187 | #[allow (deprecated)] |
| 188 | struct OldInputPinImpl { |
| 189 | state: bool, |
| 190 | } |
| 191 | |
| 192 | #[cfg (feature = "unproven" )] |
| 193 | #[allow (deprecated)] |
| 194 | impl v1::InputPin for OldInputPinImpl { |
| 195 | fn is_low(&self) -> bool { |
| 196 | !self.state |
| 197 | } |
| 198 | fn is_high(&self) -> bool { |
| 199 | self.state |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | #[cfg (feature = "unproven" )] |
| 204 | struct NewInputPinConsumer<T: v2::InputPin> { |
| 205 | _pin: T, |
| 206 | } |
| 207 | |
| 208 | #[cfg (feature = "unproven" )] |
| 209 | impl<T> NewInputPinConsumer<T> |
| 210 | where |
| 211 | T: v2::InputPin, |
| 212 | { |
| 213 | pub fn new(pin: T) -> NewInputPinConsumer<T> { |
| 214 | NewInputPinConsumer { _pin: pin } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | #[cfg (feature = "unproven" )] |
| 219 | #[test ] |
| 220 | #[cfg (feature = "unproven" )] |
| 221 | fn v2_v1_input_implicit() { |
| 222 | let i = OldInputPinImpl { state: false }; |
| 223 | let _c = NewInputPinConsumer::new(i); |
| 224 | } |
| 225 | |
| 226 | #[cfg (feature = "unproven" )] |
| 227 | #[test ] |
| 228 | fn v2_v1_input_state() { |
| 229 | let mut i = OldInputPinImpl { state: false }; |
| 230 | |
| 231 | assert_eq!(v2::InputPin::is_high(&mut i).unwrap(), false); |
| 232 | assert_eq!(v2::InputPin::is_low(&mut i).unwrap(), true); |
| 233 | } |
| 234 | } |
| 235 | |