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;
#[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,
{
pub fn new(initial: T) -> Self {
Atom { value: initial }
}
pub fn value(&self) -> &T {
&self.value
}
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())
}
}
#[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());
}
}