scsys_core/state/
mod.rs

1/*
2    Appellation: state <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5//! this module implements a generic [`State`] wrapper and provides several traits to support
6//! state management and other stateful workloads.
7//!
8#[doc(inline)]
9pub use self::{nstate::StateBase, traits::prelude::*, types::prelude::*, wrapper::State};
10/// this module implements an alternative stateful representation that enables one to provide
11/// a data type as well as specify the state _kind_
12pub mod nstate;
13pub mod wrapper;
14
15mod impls {
16    pub mod impl_wrapper;
17    pub mod impl_wrapper_ops;
18}
19
20pub mod traits {
21    //! this module implements various traits supporting the [`State`](super::State) type
22    #[doc(inline)]
23    pub use self::prelude::*;
24
25    pub mod kind;
26    pub mod state;
27    pub mod state_repr;
28    pub mod stateful;
29
30    pub(crate) mod prelude {
31        #[doc(inline)]
32        pub use super::kind::*;
33        #[doc(inline)]
34        pub use super::state::*;
35        #[doc(inline)]
36        pub use super::state_repr::*;
37        #[doc(inline)]
38        pub use super::stateful::*;
39    }
40}
41
42pub mod types {
43    //! additional types for the [`state`](crate::state) module
44    #[doc(inline)]
45    pub use self::prelude::*;
46
47    pub mod kinds;
48    pub mod nary;
49
50    pub(crate) mod prelude {
51        #[doc(inline)]
52        pub use super::aliases::*;
53        #[doc(inline)]
54        pub use super::kinds::*;
55        #[doc(inline)]
56        pub use super::nary::*;
57    }
58
59    pub(crate) mod aliases {
60        use crate::state::{Nary, StateBase};
61
62        /// A type alias for a [`StateBase`] equipped with a [`Nary`] kind of state
63        pub type NState<T, const N: usize = 4> = StateBase<T, Nary<N>>;
64    }
65}
66
67pub(crate) mod prelude {
68    #[doc(inline)]
69    pub use super::nstate::*;
70    #[doc(inline)]
71    pub use super::wrapper::*;
72
73    #[doc(inline)]
74    pub use super::traits::prelude::*;
75    #[doc(inline)]
76    pub use super::types::prelude::*;
77}
78
79#[cfg(test)]
80mod tests {
81    use super::*;
82
83    #[test]
84    fn test_state() {
85        let mut state = State::<usize>::zero();
86        // verify the initial state is zero
87        assert_eq!(state, 0);
88        // set the state to a new value
89        state.set(5);
90        // verify the state is now 5
91        assert_eq!(state, 5);
92        // take the inner value leaving the default in its place
93        assert_eq!(state.take(), 5);
94        // verify the state is now back to its default value
95        assert_eq!(state, 0);
96        // map the state to a new value
97        let mapped = state.map(|x| x + 1);
98        assert_eq!(mapped, 1);
99        // ensure the original state is unchanged
100        assert_ne!(mapped, state);
101    }
102
103    #[test]
104    fn test_state_views() {
105        let mut state = State::<usize>::zero();
106
107        let view = state.view();
108        // verify that the "view" contains a reference to the original value
109        assert_eq!(view.get(), &&0);
110        assert_eq!(view.copied(), 0);
111        // set the state to a new value
112        state.set(5);
113        // verify that the "view_mut" contains a mutable reference to the original value
114        assert_eq!(state.view().value(), &mut 5_usize);
115    }
116
117    #[cfg(feature = "rand")]
118    #[test]
119    fn test_random_state() {
120        // generate some random state `a`
121        let a = State::<f32>::random();
122        // generate another random state `b`
123        let b = State::<f32>::random();
124        // ensure the two states are not equal
125        assert_ne!(a, b);
126    }
127}