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
use crate::{NeverConflict, StateMachine};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;

/// A [StateMachine] representing a value which is "atomic" from
/// the perspective of managing state: it is only ever changed by
/// completely replacing it.
#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Debug)]
pub struct Atom<T: Copy + PartialEq + Debug> {
    value: T,
}

impl<T: Copy> Atom<T>
where
    T: 'static + Copy + Serialize + DeserializeOwned + PartialEq + Debug,
{
    /// Create a new [Atom] with a given initial value.
    pub fn new(initial: T) -> Self {
        Atom { value: initial }
    }

    /// Retrieve the current value of the atom.
    pub fn value(&self) -> &T {
        &self.value
    }

    /// Return a transition which, when processed, will replace the value of the atom
    /// with the value provided.
    pub fn replace(&self, replacement: T) -> ReplaceAtom<T> {
        ReplaceAtom(replacement)
    }
}

impl<T: Copy> StateMachine for Atom<T>
where
    T: 'static + Serialize + DeserializeOwned + Clone + PartialEq + Debug,
{
    type Transition = ReplaceAtom<T>;
    type Conflict = NeverConflict;

    fn apply(&self, transition_event: &Self::Transition) -> Result<Self, NeverConflict> {
        let ReplaceAtom(v) = transition_event;
        Ok(Atom::new(*v))
    }
}

impl<T: Copy> Default for Atom<T>
where
    T: Default + 'static + Clone + PartialEq + Debug + Serialize + DeserializeOwned,
{
    fn default() -> Self {
        Atom::new(Default::default())
    }
}

/// Represents a transition used to change the value of an [Atom].
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
pub struct ReplaceAtom<T: Copy + PartialEq + Debug>(T);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_replace() {
        let atom = Atom::new(5);
        assert_eq!(5, *atom.value());

        let atom = atom.apply(&atom.replace(8)).unwrap();

        assert_eq!(8, *atom.value());
    }
}