diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-03-27 03:33:32 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2021-03-29 00:58:58 +0200 |
| commit | a338841797e52e5a2032246ac63d29080014d16c (patch) | |
| tree | 40e8c7afa92e4e074574ea9528f4076dc253521b | |
| parent | 2c248dab56a645b37bcbd2313339bde1acd5499c (diff) | |
extras: add impl_unborrow macro
| -rw-r--r-- | embassy-extras/src/macros.rs | 21 | ||||
| -rw-r--r-- | embassy-nrf/src/gpio.rs | 30 |
2 files changed, 24 insertions, 27 deletions
diff --git a/embassy-extras/src/macros.rs b/embassy-extras/src/macros.rs index 0659a0b72..e8d7a6e3a 100644 --- a/embassy-extras/src/macros.rs +++ b/embassy-extras/src/macros.rs | |||
| @@ -84,3 +84,24 @@ macro_rules! unborrow { | |||
| 84 | )* | 84 | )* |
| 85 | } | 85 | } |
| 86 | } | 86 | } |
| 87 | |||
| 88 | #[macro_export] | ||
| 89 | macro_rules! impl_unborrow { | ||
| 90 | ($type:ident) => { | ||
| 91 | impl PeripheralBorrow for $type { | ||
| 92 | type Target = $type; | ||
| 93 | #[inline] | ||
| 94 | unsafe fn unborrow(self) -> Self::Target { | ||
| 95 | self | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | impl<'a> PeripheralBorrow for &'a mut $type { | ||
| 100 | type Target = $type; | ||
| 101 | #[inline] | ||
| 102 | unsafe fn unborrow(self) -> Self::Target { | ||
| 103 | unsafe { ::core::ptr::read(self) } | ||
| 104 | } | ||
| 105 | } | ||
| 106 | }; | ||
| 107 | } | ||
diff --git a/embassy-nrf/src/gpio.rs b/embassy-nrf/src/gpio.rs index 0d566ff3a..bda5aceff 100644 --- a/embassy-nrf/src/gpio.rs +++ b/embassy-nrf/src/gpio.rs | |||
| @@ -3,7 +3,7 @@ use core::hint::unreachable_unchecked; | |||
| 3 | use core::marker::PhantomData; | 3 | use core::marker::PhantomData; |
| 4 | 4 | ||
| 5 | use embassy::util::PeripheralBorrow; | 5 | use embassy::util::PeripheralBorrow; |
| 6 | use embassy_extras::unborrow; | 6 | use embassy_extras::{impl_unborrow, unborrow}; |
| 7 | use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin}; | 7 | use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin}; |
| 8 | use gpio::pin_cnf::DRIVE_A; | 8 | use gpio::pin_cnf::DRIVE_A; |
| 9 | 9 | ||
| @@ -305,6 +305,7 @@ impl AnyPin { | |||
| 305 | } | 305 | } |
| 306 | } | 306 | } |
| 307 | 307 | ||
| 308 | impl_unborrow!(AnyPin); | ||
| 308 | impl Pin for AnyPin {} | 309 | impl Pin for AnyPin {} |
| 309 | impl sealed::Pin for AnyPin { | 310 | impl sealed::Pin for AnyPin { |
| 310 | #[inline] | 311 | #[inline] |
| @@ -313,24 +314,6 @@ impl sealed::Pin for AnyPin { | |||
| 313 | } | 314 | } |
| 314 | } | 315 | } |
| 315 | 316 | ||
| 316 | impl PeripheralBorrow for AnyPin { | ||
| 317 | type Target = AnyPin; | ||
| 318 | #[inline] | ||
| 319 | unsafe fn unborrow(self) -> Self::Target { | ||
| 320 | self | ||
| 321 | } | ||
| 322 | } | ||
| 323 | |||
| 324 | impl<'a> PeripheralBorrow for &'a mut AnyPin { | ||
| 325 | type Target = AnyPin; | ||
| 326 | #[inline] | ||
| 327 | unsafe fn unborrow(self) -> Self::Target { | ||
| 328 | AnyPin { | ||
| 329 | pin_port: self.pin_port, | ||
| 330 | } | ||
| 331 | } | ||
| 332 | } | ||
| 333 | |||
| 334 | // ==================== | 317 | // ==================== |
| 335 | 318 | ||
| 336 | pub trait OptionalPin: sealed::OptionalPin + Sized { | 319 | pub trait OptionalPin: sealed::OptionalPin + Sized { |
| @@ -379,6 +362,7 @@ impl sealed::Pin for DummyPin { | |||
| 379 | 362 | ||
| 380 | #[derive(Clone, Copy, Debug)] | 363 | #[derive(Clone, Copy, Debug)] |
| 381 | pub struct NoPin; | 364 | pub struct NoPin; |
| 365 | impl_unborrow!(NoPin); | ||
| 382 | impl sealed::OptionalPin for NoPin {} | 366 | impl sealed::OptionalPin for NoPin {} |
| 383 | impl OptionalPin for NoPin { | 367 | impl OptionalPin for NoPin { |
| 384 | type Pin = DummyPin; | 368 | type Pin = DummyPin; |
| @@ -394,14 +378,6 @@ impl OptionalPin for NoPin { | |||
| 394 | } | 378 | } |
| 395 | } | 379 | } |
| 396 | 380 | ||
| 397 | impl PeripheralBorrow for NoPin { | ||
| 398 | type Target = NoPin; | ||
| 399 | #[inline] | ||
| 400 | unsafe fn unborrow(self) -> Self::Target { | ||
| 401 | self | ||
| 402 | } | ||
| 403 | } | ||
| 404 | |||
| 405 | // ==================== | 381 | // ==================== |
| 406 | 382 | ||
| 407 | macro_rules! make_impl { | 383 | macro_rules! make_impl { |
