extras: add impl_unborrow macro

This commit is contained in:
Dario Nieuwenhuis 2021-03-27 03:33:32 +01:00
parent 2c248dab56
commit a338841797
2 changed files with 24 additions and 27 deletions

View File

@ -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) }
}
}
};
}

View File

@ -3,7 +3,7 @@ use core::hint::unreachable_unchecked;
use core::marker::PhantomData; use core::marker::PhantomData;
use embassy::util::PeripheralBorrow; use embassy::util::PeripheralBorrow;
use embassy_extras::unborrow; use embassy_extras::{impl_unborrow, unborrow};
use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin}; use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin};
use gpio::pin_cnf::DRIVE_A; use gpio::pin_cnf::DRIVE_A;
@ -305,6 +305,7 @@ impl AnyPin {
} }
} }
impl_unborrow!(AnyPin);
impl Pin for AnyPin {} impl Pin for AnyPin {}
impl sealed::Pin for AnyPin { impl sealed::Pin for AnyPin {
#[inline] #[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 { pub trait OptionalPin: sealed::OptionalPin + Sized {
@ -379,6 +362,7 @@ impl sealed::Pin for DummyPin {
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub struct NoPin; pub struct NoPin;
impl_unborrow!(NoPin);
impl sealed::OptionalPin for NoPin {} impl sealed::OptionalPin for NoPin {}
impl OptionalPin for NoPin { impl OptionalPin for NoPin {
type Pin = DummyPin; 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 { macro_rules! make_impl {