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

/// A struct that can wrap a value so that it can be used in place
/// of a state machine, but
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
pub struct Constant<T: Clone + PartialEq + Debug + Unpin> {
    value: T,
}

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

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

impl<T> StateMachine for Constant<T>
where
    T: 'static + Serialize + DeserializeOwned + Unpin + Send + Clone + PartialEq + Debug,
{
    type Transition = InvalidTransition;

    fn apply(&mut self, _transition_event: InvalidTransition) {
        panic!("Constant should never receive transition event.");
    }
}

/// A type representing
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
pub struct InvalidTransition;

impl Transition for InvalidTransition {}

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

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