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
47
use std::{
    fmt::{Debug, Display},
    sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard},
};

/// A wrapper for `Arc<RwLock<T>>` to simplify passing complex state around callbacks.
///
/// Where possible, prefer using the ECS and storing data in (custom) components. This allows
/// other modules to introspect your module's state, and is generally easier to work with (as
/// [EntityId](crate::global::EntityId) is `Copy`, unlike this type.)
pub struct State<T: ?Sized>(Arc<RwLock<T>>);
impl<T> State<T> {
    /// Creates a new `State` with the given `value`.
    pub fn new(value: T) -> Self {
        Self(Arc::new(RwLock::new(value)))
    }

    /// Immutably borrows the state. Use this to access the state without modifying it.
    pub fn read(&self) -> RwLockReadGuard<'_, T> {
        self.0.read().unwrap()
    }

    /// Mutably borrows the state. Use this to modify the state.
    pub fn write(&self) -> RwLockWriteGuard<'_, T> {
        self.0.write().unwrap()
    }
}
impl<T: Display> Display for State<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.read().fmt(f)
    }
}
impl<T: Debug> Debug for State<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.read().fmt(f)
    }
}
impl<T: Default> Default for State<T> {
    fn default() -> Self {
        Self::new(Default::default())
    }
}
impl<T: ?Sized> Clone for State<T> {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}