rusty_mos/utils/
sync_ref_cell.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//! `RefCell` wrapper. It implements `Sync` for global use.

use core::cell::{Ref, RefCell, RefMut};

/// The wrapper.
pub struct SyncImplRef<T: ?Sized>(pub RefCell<T>);

/// For global use. The OS kernel itself is single-thread.
unsafe impl<T: ?Sized> Sync for SyncImplRef<T> {}

impl<T> SyncImplRef<T> {
    /// Default constructions. Put the target into the *interior mutability*
    /// container.
    pub const fn new(inner: T) -> Self {
        Self(RefCell::new(inner))
    }
}

impl<T: ?Sized> SyncImplRef<T> {
    /// A convenient alias for borrow.
    pub fn borrow(&self) -> Ref<'_, T> {
        self.0.borrow()
    }

    /// A convenient alias for mut borrow.
    pub fn borrow_mut(&self) -> RefMut<'_, T> {
        self.0.borrow_mut()
    }
}