sparreal_kernel/mem/
once.rs

1use core::{cell::UnsafeCell, ops::Deref};
2
3pub struct OnceStatic<T>(UnsafeCell<T>);
4
5unsafe impl<T> Send for OnceStatic<T> {}
6unsafe impl<T> Sync for OnceStatic<T> {}
7
8impl<T> OnceStatic<T> {
9    pub const fn new(v: T) -> Self {
10        OnceStatic(UnsafeCell::new(v))
11    }
12
13    /// 设置值
14    /// # Safety
15    ///
16    /// 仅在core0初始化时调用
17    pub unsafe fn set(&self, v: T) {
18        unsafe {
19            *self.0.get() = v;
20        }
21    }
22
23    /// 获取mut 引用
24    /// # Safety
25    ///
26    /// 仅在core0初始化时调用
27    pub unsafe fn get(&self) -> *mut T {
28        self.0.get()
29    }
30
31    pub fn get_ref(&self) -> &T {
32        unsafe { &*self.0.get() }
33    }
34}
35
36impl<T> Deref for OnceStatic<T> {
37    type Target = T;
38
39    fn deref(&self) -> &Self::Target {
40        unsafe { &*self.0.get() }
41    }
42}