quokka_state/
lib.rs

1#[cfg(feature = "axum")]
2pub mod extract;
3
4#[cfg(feature = "quokka-macros")]
5pub use quokka_macros::{FromState, ProvideState};
6
7///
8/// This trait allows you to create a type from a shared state object
9///
10/// It can create a new instance of itself, if `Self: Clone`
11///
12pub trait FromState<S> {
13    fn from_state(state: &S) -> Self;
14}
15
16///
17/// Provides the state S
18///
19pub trait ProvideState<S> {
20    fn provide(&self) -> S;
21}
22
23pub trait ProvideStateRef<S>: ProvideState<S> {
24    fn provide_ref(&self) -> &S;
25
26    fn provide_mut(&mut self) -> &mut S;
27}
28
29///
30/// This trait allows you to get a reference to a state object
31///
32pub trait FromStateRef<S>: FromState<S> {
33    fn from_state_ref(state: &S) -> &Self;
34
35    fn from_state_mut(state: &mut S) -> &mut Self;
36}
37
38impl<S, T: FromState<S>> ProvideState<T> for S {
39    fn provide(&self) -> T {
40        T::from_state(self)
41    }
42}
43
44impl<S, T: FromStateRef<S>> ProvideStateRef<T> for S {
45    fn provide_ref(&self) -> &T {
46        T::from_state_ref(self)
47    }
48
49    fn provide_mut(&mut self) -> &mut T {
50        T::from_state_mut(self)
51    }
52}