scsys_core/state/traits/
stateful.rs

1/*
2    appellation: stateful <module>
3    authors: @FL03
4*/
5use super::{RawState, StateRepr};
6
7/// [`Stateful`] is a trait establishing a common interface for all types that are state-aware.
8pub trait Stateful<Q>
9where
10    Q: RawState,
11{
12    type State: StateRepr<Item = Q>;
13
14    fn state(&self) -> &Self::State;
15
16    fn state_mut(&mut self) -> &mut Self::State;
17
18    fn set_state(&mut self, state: Self::State) -> &mut Self {
19        *self.state_mut() = state;
20        self
21    }
22    /// [`replace`](core::mem::replace) the current state with the given value and return the
23    /// old state.
24    fn replace_state(&mut self, state: Self::State) -> Self::State {
25        core::mem::replace(self.state_mut(), state)
26    }
27    /// [`swap`](core::mem::swap) the current state with another of the same type.
28    fn swap_state(&mut self, state: &mut Self::State) {
29        core::mem::swap(self.state_mut(), state)
30    }
31
32    fn take_state(&mut self) -> Self::State
33    where
34        Self::State: Default,
35    {
36        core::mem::take(self.state_mut())
37    }
38}