Skip to main content

maolan_engine/
mutex.rs

1use std::cell::UnsafeCell;
2
3#[derive(Debug)]
4pub struct UnsafeMutex<T> {
5    data: UnsafeCell<T>,
6}
7
8impl<T> UnsafeMutex<T> {
9    pub fn new(data: T) -> Self {
10        UnsafeMutex {
11            data: UnsafeCell::new(data),
12        }
13    }
14
15    #[allow(clippy::mut_from_ref)]
16    pub fn lock(&self) -> &mut T {
17        unsafe { &mut *self.data.get() }
18    }
19}
20
21unsafe impl<T: Send> Send for UnsafeMutex<T> {}
22unsafe impl<T: Send> Sync for UnsafeMutex<T> {}