Add Forever

This commit is contained in:
Dario Nieuwenhuis 2020-10-31 16:35:42 +01:00
parent 03bd11ce0d
commit 53eb594878
2 changed files with 35 additions and 0 deletions

View file

@ -0,0 +1,33 @@
use core::cell::UnsafeCell;
use core::mem::MaybeUninit;
use core::sync::atomic::{AtomicBool, Ordering};
pub struct Forever<T> {
used: AtomicBool,
t: UnsafeCell<MaybeUninit<T>>,
}
unsafe impl<T> Send for Forever<T> {}
unsafe impl<T> Sync for Forever<T> {}
impl<T> Forever<T> {
pub const fn new() -> Self {
Self {
used: AtomicBool::new(false),
t: UnsafeCell::new(MaybeUninit::uninit()),
}
}
pub fn put(&self, val: T) -> &'static mut T {
if self.used.compare_and_swap(false, true, Ordering::SeqCst) {
panic!("Forever.put() called multiple times");
}
unsafe {
let p = self.t.get();
let p = (&mut *p).as_mut_ptr();
p.write(val);
&mut *p
}
}
}

View file

@ -10,6 +10,8 @@ mod waker_store;
pub use waker_store::*; pub use waker_store::*;
mod drop_bomb; mod drop_bomb;
pub use drop_bomb::*; pub use drop_bomb::*;
mod forever;
pub use forever::*;
use defmt::{debug, error, info, intern, trace, warn}; use defmt::{debug, error, info, intern, trace, warn};