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
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
use serde::{Deserialize, Serialize};
use std::collections::BTreeSet;

use crate::{CmRDT, CvRDT};

/// A `GSet` is a grow-only set.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct GSet<T: Ord> {
    value: BTreeSet<T>,
}

impl<T: Ord> Default for GSet<T> {
    fn default() -> Self {
        GSet::new()
    }
}

impl<T: Ord> From<GSet<T>> for BTreeSet<T> {
    fn from(gset: GSet<T>) -> BTreeSet<T> {
        gset.value
    }
}

impl<T: Ord + Clone> CvRDT for GSet<T> {
    /// Merges another `GSet` into this one.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use crdts::{GSet, CvRDT, CmRDT};
    /// let (mut a, mut b) = (GSet::new(), GSet::new());
    /// a.insert(1);
    /// b.insert(2);
    /// a.merge(b);
    /// assert!(a.contains(&1));
    /// assert!(a.contains(&2));
    /// ```
    fn merge(&mut self, other: Self) {
        other.value.into_iter().for_each(|e| self.insert(e))
    }
}

impl<T: Ord> CmRDT for GSet<T> {
    type Op = T;

    fn apply(&mut self, op: Self::Op) {
        self.insert(op);
    }
}

impl<T: Ord> GSet<T> {
    /// Instantiates an empty `GSet`.
    pub fn new() -> Self {
        Self {
            value: BTreeSet::new(),
        }
    }

    /// Inserts an element into this `GSet`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use crdts::GSet;
    /// let mut a = GSet::new();
    /// a.insert(1);
    /// assert!(a.contains(&1));
    /// ```
    pub fn insert(&mut self, element: T) {
        self.value.insert(element);
    }

    /// Returns `true` if the `GSet` contains the element.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use crdts::GSet;
    /// let mut a = GSet::new();
    /// a.insert(1);
    /// assert!(a.contains(&1));
    /// ```
    pub fn contains(&self, element: &T) -> bool {
        self.value.contains(element)
    }

    /// Returns the `BTreeSet` for this `GSet`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use crdts::GSet;
    /// use std::collections::BTreeSet;
    /// let mut a = GSet::new();
    /// let mut b = BTreeSet::new();
    /// for i in 1..10 {
    ///     a.insert(i);
    ///     b.insert(i);
    /// }
    ///
    /// assert_eq!(a.read(), b);
    /// ```
    pub fn read(&self) -> BTreeSet<T>
    where
        T: Clone,
    {
        self.value.clone()
    }
}