Add Forever
This commit is contained in:
parent
03bd11ce0d
commit
53eb594878
33
embassy/src/util/forever.rs
Normal file
33
embassy/src/util/forever.rs
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
@ -10,6 +10,8 @@ mod waker_store;
|
||||
pub use waker_store::*;
|
||||
mod drop_bomb;
|
||||
pub use drop_bomb::*;
|
||||
mod forever;
|
||||
pub use forever::*;
|
||||
|
||||
use defmt::{debug, error, info, intern, trace, warn};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user