Skip to main content

omena_reactive/
policy.rs

1use std::fmt;
2
3use crate::ReactiveStateV0;
4
5pub type ChangeComparatorV0 = fn(&ReactiveStateV0, &ReactiveStateV0) -> bool;
6
7/// A named semantic equivalence relation used to decide whether a new node
8/// value should propagate.
9///
10/// There is intentionally no `Default` implementation. A graph constructor
11/// cannot create a node without selecting a policy.
12#[derive(Clone, Copy)]
13pub struct ChangePolicyV0 {
14    name: &'static str,
15    equivalent: ChangeComparatorV0,
16}
17
18impl ChangePolicyV0 {
19    pub const fn exact(name: &'static str) -> Self {
20        Self {
21            name,
22            equivalent: ReactiveStateV0::eq,
23        }
24    }
25
26    pub const fn custom(name: &'static str, equivalent: ChangeComparatorV0) -> Self {
27        Self { name, equivalent }
28    }
29
30    pub fn name(self) -> &'static str {
31        self.name
32    }
33
34    pub fn equivalent(self, previous: &ReactiveStateV0, next: &ReactiveStateV0) -> bool {
35        (self.equivalent)(previous, next)
36    }
37}
38
39impl fmt::Debug for ChangePolicyV0 {
40    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
41        formatter
42            .debug_struct("ChangePolicyV0")
43            .field("name", &self.name)
44            .finish_non_exhaustive()
45    }
46}