fuel_core_services/
sync.rs1use core::ops::Deref;
4
5pub type Shared<T> = std::sync::Arc<T>;
7
8#[derive(Default, Debug)]
10pub struct SharedMutex<T>(Shared<parking_lot::Mutex<T>>);
11
12impl<T> Clone for SharedMutex<T> {
13 fn clone(&self) -> Self {
14 Self(self.0.clone())
15 }
16}
17
18impl<T> Deref for SharedMutex<T> {
19 type Target = Shared<parking_lot::Mutex<T>>;
20
21 fn deref(&self) -> &Self::Target {
22 &self.0
23 }
24}
25
26impl<T> SharedMutex<T> {
27 pub fn new(t: T) -> Self {
29 Self(Shared::new(parking_lot::Mutex::new(t)))
30 }
31
32 pub fn apply<R>(&self, f: impl FnOnce(&mut T) -> R) -> R {
34 let mut t = self.0.lock();
35 f(&mut t)
36 }
37}
38
39impl<T> From<T> for SharedMutex<T> {
40 fn from(t: T) -> Self {
41 Self::new(t)
42 }
43}
44
45#[derive(Default, Debug)]
47pub struct SharedRwLock<T>(Shared<parking_lot::RwLock<T>>);
48
49impl<T> Clone for SharedRwLock<T> {
50 fn clone(&self) -> Self {
51 Self(self.0.clone())
52 }
53}
54
55impl<T> Deref for SharedRwLock<T> {
56 type Target = Shared<parking_lot::RwLock<T>>;
57
58 fn deref(&self) -> &Self::Target {
59 &self.0
60 }
61}
62
63impl<T> SharedRwLock<T> {
64 pub fn new(t: T) -> Self {
66 Self(Shared::new(parking_lot::RwLock::new(t)))
67 }
68}
69
70impl<T> From<T> for SharedRwLock<T> {
71 fn from(t: T) -> Self {
72 Self::new(t)
73 }
74}