From 6aa0fb450f417ce899b43f4539eb226b391a0f2e Mon Sep 17 00:00:00 2001 From: Emil Fresk Date: Thu, 8 Apr 2021 18:25:09 +0200 Subject: Goodbye static mut --- src/lib.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index a4abc4c..cd51199 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -57,3 +57,31 @@ where { NVIC::pend(interrupt) } + +use core::cell::UnsafeCell; + +/// Internal replacement for `static mut T` +#[repr(transparent)] +pub struct RacyCell(UnsafeCell); + +impl RacyCell { + /// Create a RacyCell + #[inline(always)] + pub const fn new(value: T) -> Self { + RacyCell(UnsafeCell::new(value)) + } + + /// Get `&mut T` + #[inline(always)] + pub unsafe fn get_mut_unchecked(&self) -> &mut T { + &mut *self.0.get() + } + + /// Get `&T` + #[inline(always)] + pub unsafe fn get_unchecked(&self) -> &T { + &*self.0.get() + } +} + +unsafe impl Sync for RacyCell {} -- cgit v1.2.3