diff options
author | 2021-04-08 18:25:09 +0200 | |
---|---|---|
committer | 2021-04-08 19:58:20 +0200 | |
commit | 6aa0fb450f417ce899b43f4539eb226b391a0f2e (patch) | |
tree | 2202c8bb4aa2ba2451f025784a5bad99c4370b2e /src | |
parent | 43c5ad79c27fbdefa00e2373eba554ec11e1d9df (diff) | |
download | rtic-6aa0fb450f417ce899b43f4539eb226b391a0f2e.tar.gz rtic-6aa0fb450f417ce899b43f4539eb226b391a0f2e.tar.zst rtic-6aa0fb450f417ce899b43f4539eb226b391a0f2e.zip |
Goodbye static mut
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 28 |
1 files changed, 28 insertions, 0 deletions
@@ -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> {} |