os/sync/
up.rs

1//! Uniprocessor interior mutability primitives
2use core::cell::{RefCell, RefMut};
3
4/// Wrap a static data structure inside it so that we are
5/// able to access it without any `unsafe`.
6///
7/// We should only use it in uniprocessor.
8///
9/// In order to get mutable reference of inner data, call
10/// `exclusive_access`.
11pub struct UPSafeCell<T> {
12    /// inner data
13    inner: RefCell<T>,
14}
15
16unsafe impl<T> Sync for UPSafeCell<T> {}
17
18impl<T> UPSafeCell<T> {
19    /// User is responsible to guarantee that inner struct is only used in
20    /// uniprocessor.
21    pub unsafe fn new(value: T) -> Self {
22        Self {
23            inner: RefCell::new(value),
24        }
25    }
26    /// Exclusive access inner data in UPSafeCell. Panic if the data has been borrowed.
27    pub fn exclusive_access(&self) -> RefMut<'_, T> {
28        self.inner.borrow_mut()
29    }
30}