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
//! Derive node requirements for primitive types.
//!
//! Primitives all implement `UpdateInput` with a simple replace.

macro_rules! impl_for_primitive {
    ($($ty:ty),*) => {
        $(
            impl crate::execution::Named for $ty {
                fn name() -> &'static str {
                    stringify!($ty)
                }
            }

            impl crate::execution::HashValue for $ty {
                fn hash_value(&self, hasher: &mut impl std::hash::Hasher) -> crate::execution::NodeHash {
                    use std::hash::Hash;
                    crate::execution::NodeHash::Hashed({
                        self.hash(hasher);
                        hasher.finish()
                    })
                }
            }

            impl crate::execution::Clean for $ty {
                fn clean(&mut self) {}
            }

            impl crate::execution::UpdateInput for $ty {
                type Update = Self;

                fn update_mut(&mut self, update: Self::Update) {
                    *self = update;
                }
            }
        )*
    };
}

impl_for_primitive! {
    u8, u16, u32, u64, u128, usize,
    i8, i16, i32, i64, i128, isize,
    bool,
    char,
    String
}

#[cfg(test)]
mod tests {
    use std::collections::hash_map::DefaultHasher;

    use crate::execution::{Clean, HashValue, Named};

    #[test]
    fn test_hash_value() {
        let mut hasher = DefaultHasher::new();
        let hash = 42u32.hash_value(&mut hasher);
        assert_eq!(
            hash,
            crate::execution::NodeHash::Hashed(15387811073369036852)
        );
    }

    #[test]
    fn test_name() {
        assert_eq!(u32::name(), "u32");
    }

    #[test]
    fn test_clean() {
        let mut value = 420u16;
        value.clean();
        assert_eq!(value, 420u16);
    }
}