loom_core_actors/
shared_state.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use std::sync::Arc;

use eyre::Result;
use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard, TryLockError};

//#[derive(Clone)]
pub struct SharedState<T> {
    inner: Arc<RwLock<T>>,
}

impl<T> SharedState<T> {
    pub fn new(shared_data: T) -> SharedState<T> {
        SharedState { inner: Arc::new(RwLock::new(shared_data)) }
    }

    pub async fn read(&self) -> RwLockReadGuard<T> {
        self.inner.read().await
    }

    pub fn try_read(&self) -> Result<RwLockReadGuard<T>, TryLockError> {
        self.inner.try_read()
    }

    pub async fn write(&self) -> RwLockWriteGuard<T> {
        self.inner.write().await
    }

    pub fn try_write(&self) -> Result<RwLockWriteGuard<T>, TryLockError> {
        self.inner.try_write()
    }

    pub fn inner(&self) -> Arc<RwLock<T>> {
        self.inner.clone()
    }

    pub async fn update(&self, inner: T) {
        let mut guard = self.inner.write().await;
        *guard = inner
    }
}

impl<T> Clone for SharedState<T> {
    fn clone(&self) -> Self {
        SharedState { inner: self.inner().clone() }
    }
}