use core::cell::{RefCell, RefMut}; use core::ops::{Deref, DerefMut}; pub type LockResult = Result; pub struct Mutex { inner: RefCell } #[must_use = "if unused the Mutex will immediately unlock"] pub struct MutexGuard<'a, T: ?Sized + 'a> { lock: RefMut<'a, T>, } impl Deref for MutexGuard<'_, T> { type Target = T; fn deref(&self) -> &T { &self.lock.deref() } } impl DerefMut for MutexGuard<'_, T> { fn deref_mut(&mut self) -> &mut T { self.lock.deref_mut() } } impl Mutex { pub fn new(inner: T) -> Mutex { Mutex { inner: RefCell::new(inner) } } pub fn lock<'a>(&'a self) -> LockResult> { Ok(MutexGuard { lock: self.inner.borrow_mut() }) } }