cvrdt_exposition/
one_way_boolean.rs

1use crate::traits::Grow;
2
3/// A boolean flag that, once true, can never revert to false
4///
5/// # Examples
6///
7/// Example usage, including demonstrating some properties:
8///
9/// ```
10/// use cvrdt_exposition::{Grow, OneWayBoolean};
11/// let mut x = OneWayBoolean::new(false);
12/// x.add(());
13/// assert_eq!(x.payload(), true);
14/// assert_eq!(x.query(&()), true);
15/// for payload in vec![true, false] {
16///     let y = OneWayBoolean::new(payload);
17///     assert_eq!(x.merge(&y).payload(), y.merge(&x).payload());
18///     assert!(y.le(&x));
19/// }
20/// ```
21#[derive(Debug, Clone)]
22pub struct OneWayBoolean {
23    /// The internal state of a `OneWayBoolean` is a single boolean flag
24    pub flag: bool,
25}
26
27impl Grow for OneWayBoolean {
28    type Payload = bool;
29    type Update = ();
30    type Query = ();
31    type Value = bool;
32
33    fn new(payload: Self::Payload) -> Self {
34        OneWayBoolean { flag: payload }
35    }
36    fn payload(&self) -> Self::Payload {
37        self.flag
38    }
39    fn add(&mut self, _update: Self::Update) {
40        self.flag = true;
41    }
42    fn le(&self, other: &OneWayBoolean) -> bool {
43        self.flag <= other.flag
44    }
45    fn merge(&self, other: &OneWayBoolean) -> Self {
46        OneWayBoolean {
47            flag: self.flag || other.flag,
48        }
49    }
50    fn query(&self, _query: &Self::Query) -> Self::Value {
51        self.flag
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58    use crate::properties::grow;
59    use proptest::prelude::*;
60
61    fn cvrdt() -> impl Strategy<Value = OneWayBoolean> {
62        any::<bool>().prop_map(|flag| OneWayBoolean { flag })
63    }
64
65    fn cvrdt_and_update() -> impl Strategy<Value = (OneWayBoolean, ())> {
66        (cvrdt(), Just(()))
67    }
68
69    grow!(cvrdt, cvrdt_and_update);
70}