rstm_state/traits/
stateful.rs

1/*
2    Appellation: stateful <module>
3    Created At: 2025.08.31:08:47:19
4    Contrib: @FL03
5*/
6use crate::RawState;
7
8/// The [`StateRepr`] trait defines a common interface for any implemented representations of
9/// state.
10pub trait StateRepr<U>
11where
12    U: RawState,
13{
14    type Repr<_V: RawState>;
15
16    private!();
17    /// return a reference to the underlying state
18    fn get(&self) -> &U;
19    /// return a mutable reference to the underlying state
20    fn get_mut(&mut self) -> &mut U;
21    /// returns a _view_ of the representation using a reference to the current value
22    fn view(&self) -> Self::Repr<&U>;
23    /// returns a _view_ of the representation containing a mutable reference to the current
24    /// value
25    fn view_mut(&mut self) -> Self::Repr<&mut U>;
26    /// [`replace`](core::mem::replace) the inner value with another value, returning the old value
27    fn replace(&mut self, value: U) -> U {
28        core::mem::replace(self.get_mut(), value)
29    }
30    /// update the inner value
31    fn set(&mut self, value: U) {
32        *self.get_mut() = value;
33    }
34    /// [`swap`](core::mem::swap) the inner value with another value
35    fn swap(&mut self, other: &mut Self) {
36        core::mem::swap(self.get_mut(), other.get_mut())
37    }
38    /// [`take`](core::mem::take) the inner value, leaving a default value in its place
39    fn take(&mut self) -> U
40    where
41        U: Default,
42    {
43        core::mem::take(self.get_mut())
44    }
45}