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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use crate::states::{find_state, find_state_unwrap, insert_state, insert_state_if_not_exists};
use std::error::Error;
pub trait InitAppState {
fn init_app_state(self);
}
pub trait InitMutAppState {
fn init_mut_app_state(self);
}
pub trait CreateAppState<T: 'static + Send> {
fn new(state: T) -> Self;
}
pub trait AppStateTrait<T, U>
where
T: 'static + Send,
U: 'static + AppStateTrait<T, U> + CreateAppState<T> + Clone + Send,
{
/// Initializes the state store with the given state.
/// If the state store has already been initialized, this will overwrite the existing state.
///
/// # Examples
/// ```rust
/// use app_state::{AppState, AppStateTrait};
///
/// struct MyState {
/// counter: u32,
/// }
///
/// fn main() {
/// let state = MyState { counter: 0 };
/// AppState::init(state);
/// }
/// ```
fn init(state: T) {
#[cfg(feature = "log")]
log::debug!("Initializing state {}", std::any::type_name::<T>());
insert_state(U::new(state));
}
/// Initializes the state store with the given state.
/// If the state store has already been initialized, this will do nothing.
/// This is useful for initializing the state store with a default state.
/// If you want to overwrite the existing state, use `init` instead.
///
/// # Examples
/// ```rust
/// use app_state::{AppState, AppStateTrait};
///
/// struct MyState {
/// counter: u32,
/// }
///
/// fn main() {
/// AppState::init_if_not_exists(|| MyState { counter: 0 });
/// }
/// ```
fn init_if_not_exists<F: FnOnce() -> T>(state: F) {
insert_state_if_not_exists(|| {
#[cfg(feature = "log")]
log::debug!("Initializing state {}", std::any::type_name::<T>());
U::new(state())
});
}
/// Returns a reference to the state.
/// If the state store has not been initialized, this will panic.
fn get() -> U {
find_state_unwrap()
}
/// Returns a reference to the state.
/// If the state store has not been initialized, this will return `Err`.
fn try_get() -> Result<U, Box<dyn Error>> {
find_state()
}
/// Returns a reference to the state.
/// Inserts the supplied value if the state store has not been initialized.
fn get_or_insert(val: T) -> U {
insert_state_if_not_exists(|| {
#[cfg(feature = "log")]
log::debug!("Initializing state {}", std::any::type_name::<T>());
U::new(val)
})
}
/// Returns a reference to the state.
/// Inserts the supplied value if the state store has not been initialized.
fn get_or_insert_with<F: FnOnce() -> T>(f: F) -> U {
insert_state_if_not_exists(|| {
#[cfg(feature = "log")]
log::debug!("Initializing state {}", std::any::type_name::<T>());
U::new(f())
})
}
/// Returns a reference to the state.
/// Inserts the default value of `T` if the state store has not been initialized.
fn get_or_insert_default() -> U
where
T: Default,
{
insert_state_if_not_exists(|| {
#[cfg(feature = "log")]
log::debug!("Initializing state {}", std::any::type_name::<T>());
U::new(T::default())
})
}
}