From 41e392bda30feae7e57f32872be082b1f15392ac Mon Sep 17 00:00:00 2001 From: Grant Miller Date: Mon, 25 Jul 2022 22:30:01 -0500 Subject: [PATCH 1/2] Use `Forever::put_with` inside `Forever::put` --- embassy/src/util/forever.rs | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/embassy/src/util/forever.rs b/embassy/src/util/forever.rs index ba3c66a9..4c262005 100644 --- a/embassy/src/util/forever.rs +++ b/embassy/src/util/forever.rs @@ -52,20 +52,7 @@ impl Forever { #[inline(always)] #[allow(clippy::mut_from_ref)] pub fn put(&'static self, val: T) -> &'static mut T { - if self - .used - .compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed) - .is_err() - { - panic!("Forever::put() called multiple times"); - } - - unsafe { - let p = self.t.get(); - let p = (&mut *p).as_mut_ptr(); - p.write(val); - &mut *p - } + self.put_with(|| val) } /// Store the closure return value in this `Forever`, returning a mutable reference to it. From 1d63a30d5f52cc69b4dd606d0f2a12fe4e03f055 Mon Sep 17 00:00:00 2001 From: Grant Miller Date: Mon, 25 Jul 2022 23:09:07 -0500 Subject: [PATCH 2/2] Forever: Simplify pointer handling --- embassy/src/util/forever.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/embassy/src/util/forever.rs b/embassy/src/util/forever.rs index 4c262005..3d2af38b 100644 --- a/embassy/src/util/forever.rs +++ b/embassy/src/util/forever.rs @@ -74,12 +74,8 @@ impl Forever { panic!("Forever.put() called multiple times"); } - unsafe { - let p = self.t.get(); - let p = (&mut *p).as_mut_ptr(); - p.write(val()); - &mut *p - } + let p: &'static mut MaybeUninit = unsafe { &mut *self.t.get() }; + p.write(val()) } /// Unsafely get a mutable reference to the contents of this Forever. @@ -93,8 +89,7 @@ impl Forever { #[inline(always)] #[allow(clippy::mut_from_ref)] pub unsafe fn steal(&self) -> &mut T { - let p = self.t.get(); - let p = (&mut *p).as_mut_ptr(); - &mut *p + let p: &mut MaybeUninit = &mut *self.t.get(); + p.assume_init_mut() } }