diff --git a/embassy-extras/src/macros.rs b/embassy-extras/src/macros.rs index 0659a0b7..e8d7a6e3 100644 --- a/embassy-extras/src/macros.rs +++ b/embassy-extras/src/macros.rs @@ -84,3 +84,24 @@ macro_rules! unborrow { )* } } + +#[macro_export] +macro_rules! impl_unborrow { + ($type:ident) => { + impl PeripheralBorrow for $type { + type Target = $type; + #[inline] + unsafe fn unborrow(self) -> Self::Target { + self + } + } + + impl<'a> PeripheralBorrow for &'a mut $type { + type Target = $type; + #[inline] + unsafe fn unborrow(self) -> Self::Target { + unsafe { ::core::ptr::read(self) } + } + } + }; +} diff --git a/embassy-nrf/src/gpio.rs b/embassy-nrf/src/gpio.rs index 0d566ff3..bda5acef 100644 --- a/embassy-nrf/src/gpio.rs +++ b/embassy-nrf/src/gpio.rs @@ -3,7 +3,7 @@ use core::hint::unreachable_unchecked; use core::marker::PhantomData; use embassy::util::PeripheralBorrow; -use embassy_extras::unborrow; +use embassy_extras::{impl_unborrow, unborrow}; use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin}; use gpio::pin_cnf::DRIVE_A; @@ -305,6 +305,7 @@ impl AnyPin { } } +impl_unborrow!(AnyPin); impl Pin for AnyPin {} impl sealed::Pin for AnyPin { #[inline] @@ -313,24 +314,6 @@ impl sealed::Pin for AnyPin { } } -impl PeripheralBorrow for AnyPin { - type Target = AnyPin; - #[inline] - unsafe fn unborrow(self) -> Self::Target { - self - } -} - -impl<'a> PeripheralBorrow for &'a mut AnyPin { - type Target = AnyPin; - #[inline] - unsafe fn unborrow(self) -> Self::Target { - AnyPin { - pin_port: self.pin_port, - } - } -} - // ==================== pub trait OptionalPin: sealed::OptionalPin + Sized { @@ -379,6 +362,7 @@ impl sealed::Pin for DummyPin { #[derive(Clone, Copy, Debug)] pub struct NoPin; +impl_unborrow!(NoPin); impl sealed::OptionalPin for NoPin {} impl OptionalPin for NoPin { type Pin = DummyPin; @@ -394,14 +378,6 @@ impl OptionalPin for NoPin { } } -impl PeripheralBorrow for NoPin { - type Target = NoPin; - #[inline] - unsafe fn unborrow(self) -> Self::Target { - self - } -} - // ==================== macro_rules! make_impl {