]> git.bitcoin.ninja Git - rust-lightning/blob - lightning-invoice/src/sync.rs
Merge pull request #1706 from jkczyz/2022-09-filtered-blocks
[rust-lightning] / lightning-invoice / src / sync.rs
1 use core::cell::{RefCell, RefMut};
2 use core::ops::{Deref, DerefMut};
3
4 pub type LockResult<Guard> = Result<Guard, ()>;
5
6 pub struct Mutex<T: ?Sized> {
7         inner: RefCell<T>
8 }
9
10 #[must_use = "if unused the Mutex will immediately unlock"]
11 pub struct MutexGuard<'a, T: ?Sized + 'a> {
12         lock: RefMut<'a, T>,
13 }
14
15 impl<T: ?Sized> Deref for MutexGuard<'_, T> {
16         type Target = T;
17
18         fn deref(&self) -> &T {
19                 &self.lock.deref()
20         }
21 }
22
23 impl<T: ?Sized> DerefMut for MutexGuard<'_, T> {
24         fn deref_mut(&mut self) -> &mut T {
25                 self.lock.deref_mut()
26         }
27 }
28
29 impl<T> Mutex<T> {
30         pub fn new(inner: T) -> Mutex<T> {
31                 Mutex { inner: RefCell::new(inner) }
32         }
33
34         pub fn lock<'a>(&'a self) -> LockResult<MutexGuard<'a, T>> {
35                 Ok(MutexGuard { lock: self.inner.borrow_mut() })
36         }
37 }