From a338841797e52e5a2032246ac63d29080014d16c Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Sat, 27 Mar 2021 03:33:32 +0100 Subject: [PATCH] extras: add impl_unborrow macro --- embassy-extras/src/macros.rs | 21 +++++++++++++++++++++ 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 { )* } } + +#[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 0d566ff3a..bda5aceff 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 {