aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2021-03-27 03:33:32 +0100
committerDario Nieuwenhuis <[email protected]>2021-03-29 00:58:58 +0200
commita338841797e52e5a2032246ac63d29080014d16c (patch)
tree40e8c7afa92e4e074574ea9528f4076dc253521b
parent2c248dab56a645b37bcbd2313339bde1acd5499c (diff)
extras: add impl_unborrow macro
-rw-r--r--embassy-extras/src/macros.rs21
-rw-r--r--embassy-nrf/src/gpio.rs30
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]
89macro_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;
3use core::marker::PhantomData; 3use core::marker::PhantomData;
4 4
5use embassy::util::PeripheralBorrow; 5use embassy::util::PeripheralBorrow;
6use embassy_extras::unborrow; 6use embassy_extras::{impl_unborrow, unborrow};
7use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin}; 7use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin};
8use gpio::pin_cnf::DRIVE_A; 8use gpio::pin_cnf::DRIVE_A;
9 9
@@ -305,6 +305,7 @@ impl AnyPin {
305 } 305 }
306} 306}
307 307
308impl_unborrow!(AnyPin);
308impl Pin for AnyPin {} 309impl Pin for AnyPin {}
309impl sealed::Pin for AnyPin { 310impl sealed::Pin for AnyPin {
310 #[inline] 311 #[inline]
@@ -313,24 +314,6 @@ impl sealed::Pin for AnyPin {
313 } 314 }
314} 315}
315 316
316impl PeripheralBorrow for AnyPin {
317 type Target = AnyPin;
318 #[inline]
319 unsafe fn unborrow(self) -> Self::Target {
320 self
321 }
322}
323
324impl<'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
336pub trait OptionalPin: sealed::OptionalPin + Sized { 319pub 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)]
381pub struct NoPin; 364pub struct NoPin;
365impl_unborrow!(NoPin);
382impl sealed::OptionalPin for NoPin {} 366impl sealed::OptionalPin for NoPin {}
383impl OptionalPin for NoPin { 367impl 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
397impl 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
407macro_rules! make_impl { 383macro_rules! make_impl {