Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
Use `merge` as the `Semilattice` join method
Alexis Sellier committed 3 years ago
commit 0448441418579ef34259c18026ca1857b17e2a8f
parent 01b7686659356abd60d71169cc54b05d80b238b6
6 files changed +21 -30
modified radicle-crdt/src/lib.rs
@@ -15,9 +15,18 @@ mod test;
////////////////////////////////////////////////////////////////////////////////

/// A join-semilattice.
-
pub trait Semilattice {
-
    /// Join or "merge" two semilattices into one.
-
    fn join(self, other: Self) -> Self;
+
pub trait Semilattice: Sized {
+
    /// Merge an other semilattice into this one.
+
    ///
+
    /// This operation should obbey the semilattice laws and should thus be idempotent,
+
    /// associative and commutative.
+
    fn merge(&mut self, other: Self);
+

+
    /// Like [`Semilattice::merge`] but takes and returns a new semilattice.
+
    fn join(mut self, other: Self) -> Self {
+
        self.merge(other);
+
        self
+
    }
}

/// Reduce an iterator of semilattice values to its least upper bound.
modified radicle-crdt/src/lwwmap.rs
@@ -90,7 +90,7 @@ where
    V: PartialOrd + Eq,
    C: Ord + Copy + Default,
{
-
    fn join(mut self, other: Self) -> Self {
+
    fn merge(&mut self, other: Self) {
        for (k, v) in other.inner.into_iter() {
            match self.inner.entry(k) {
                Entry::Occupied(mut e) => {
@@ -101,7 +101,6 @@ where
                }
            }
        }
-
        self
    }
}

modified radicle-crdt/src/lwwreg.rs
@@ -33,10 +33,6 @@ impl<T: PartialOrd, C: PartialOrd> LWWReg<T, C> {
        let (t, c) = self.inner.into_inner();
        (c, t)
    }
-

-
    pub fn merge(&mut self, other: Self) {
-
        self.inner.merge(other.inner);
-
    }
}

impl<T, C> Semilattice for LWWReg<T, C>
@@ -44,10 +40,8 @@ where
    T: PartialOrd + Default,
    C: PartialOrd + Default,
{
-
    fn join(self, other: Self) -> Self {
-
        Self {
-
            inner: self.inner.join(other.inner),
-
        }
+
    fn merge(&mut self, other: Self) {
+
        self.inner.merge(other.inner);
    }
}

modified radicle-crdt/src/lwwset.rs
@@ -61,10 +61,8 @@ where
    T: Ord,
    C: Ord + Copy + Default,
{
-
    fn join(self, other: Self) -> Self {
-
        Self {
-
            inner: self.inner.join(other.inner),
-
        }
+
    fn merge(&mut self, other: Self) {
+
        self.inner.merge(other.inner);
    }
}

modified radicle-crdt/src/ord.rs
@@ -57,11 +57,9 @@ impl<T> From<T> for Max<T> {
}

impl<T: PartialOrd> Semilattice for Max<T> {
-
    fn join(self, other: Self) -> Self {
+
    fn merge(&mut self, other: Self) {
        if other.0 > self.0 {
-
            other
-
        } else {
-
            self
+
            self.0 = other.0;
        }
    }
}
modified radicle-crdt/src/redactable.rs
@@ -19,8 +19,8 @@ impl<T> From<Option<T>> for Redactable<T> {
    }
}

-
impl<T: PartialOrd> Redactable<T> {
-
    pub fn merge(&mut self, other: Self) {
+
impl<T: PartialOrd> Semilattice for Redactable<T> {
+
    fn merge(&mut self, other: Self) {
        match (&self, other) {
            (Self::Redacted, _) => {}
            (Self::Present(_), Self::Redacted) => {
@@ -35,13 +35,6 @@ impl<T: PartialOrd> Redactable<T> {
    }
}

-
impl<T: PartialOrd> Semilattice for Redactable<T> {
-
    fn join(mut self, other: Self) -> Self {
-
        self.merge(other);
-
        self
-
    }
-
}
-

#[cfg(test)]
mod test {
    use quickcheck_macros::quickcheck;