From dc67d2f4a49f4c9166fdefa4319a0e6dfab823e8 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Wed, 19 May 2021 22:31:09 +0200 Subject: [PATCH] impl Unborrow for &'a mut T This plays nicer with user code that's generic over peripheral traits like `Instance` or `Pin`. --- embassy-extras/src/macros.rs | 16 ---------------- embassy-macros/src/lib.rs | 7 ------- embassy/src/util/mod.rs | 18 +++++++----------- 3 files changed, 7 insertions(+), 34 deletions(-) diff --git a/embassy-extras/src/macros.rs b/embassy-extras/src/macros.rs index fba75261..351938c4 100644 --- a/embassy-extras/src/macros.rs +++ b/embassy-extras/src/macros.rs @@ -24,14 +24,6 @@ macro_rules! peripherals { } } - $(#[$cfg])? - impl embassy::util::Unborrow for &mut $name { - type Target = $name; - #[inline] - unsafe fn unborrow(self) -> $name { - ::core::ptr::read(self) - } - } )* } @@ -95,14 +87,6 @@ macro_rules! impl_unborrow { self } } - - impl<'a> ::embassy::util::Unborrow for &'a mut $type { - type Target = $type; - #[inline] - unsafe fn unborrow(self) -> Self::Target { - unsafe { ::core::ptr::read(self) } - } - } }; } diff --git a/embassy-macros/src/lib.rs b/embassy-macros/src/lib.rs index c2f928b9..ac856bef 100644 --- a/embassy-macros/src/lib.rs +++ b/embassy-macros/src/lib.rs @@ -216,13 +216,6 @@ pub fn interrupt_declare(item: TokenStream) -> TokenStream { self } } - - impl ::embassy::util::Unborrow for &mut #name_interrupt { - type Target = #name_interrupt; - unsafe fn unborrow(self) -> #name_interrupt { - ::core::ptr::read(self) - } - } }; result.into() } diff --git a/embassy/src/util/mod.rs b/embassy/src/util/mod.rs index 7de15d4a..8057e120 100644 --- a/embassy/src/util/mod.rs +++ b/embassy/src/util/mod.rs @@ -22,6 +22,13 @@ pub trait Unborrow { unsafe fn unborrow(self) -> Self::Target; } +impl<'a, T: Unborrow> Unborrow for &'a mut T { + type Target = T::Target; + unsafe fn unborrow(self) -> Self::Target { + T::unborrow(core::ptr::read(self)) + } +} + pub trait Steal { unsafe fn steal() -> Self; } @@ -40,17 +47,6 @@ macro_rules! impl_unborrow_tuples { } } - impl<'a, $($t),+> Unborrow for &'a mut($($t),+) - where - $( - $t: Unborrow - ),+ - { - type Target = ($($t),+); - unsafe fn unborrow(self) -> Self::Target { - ::core::ptr::read(self) - } - } }; }