Goodbye static mut

This commit is contained in:
Emil Fresk 2021-04-08 18:25:09 +02:00
parent 43c5ad79c2
commit 6aa0fb450f
11 changed files with 145 additions and 59 deletions

View file

@ -57,3 +57,31 @@ where
{
NVIC::pend(interrupt)
}
use core::cell::UnsafeCell;
/// Internal replacement for `static mut T`
#[repr(transparent)]
pub struct RacyCell<T>(UnsafeCell<T>);
impl<T> RacyCell<T> {
/// 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<T> Sync for RacyCell<T> {}