From 0448441418579ef34259c18026ca1857b17e2a8f Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Thu, 24 Nov 2022 19:10:32 +0100 Subject: [PATCH] Use `merge` as the `Semilattice` join method Signed-off-by: Alexis Sellier --- radicle-crdt/src/lib.rs | 15 ++++++++++++--- radicle-crdt/src/lwwmap.rs | 3 +-- radicle-crdt/src/lwwreg.rs | 10 ++-------- radicle-crdt/src/lwwset.rs | 6 ++---- radicle-crdt/src/ord.rs | 6 ++---- radicle-crdt/src/redactable.rs | 11 ++--------- 6 files changed, 21 insertions(+), 30 deletions(-) diff --git a/radicle-crdt/src/lib.rs b/radicle-crdt/src/lib.rs index e6d46cda..2d3afc58 100644 --- a/radicle-crdt/src/lib.rs +++ b/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. diff --git a/radicle-crdt/src/lwwmap.rs b/radicle-crdt/src/lwwmap.rs index c42aa5ce..6792eb38 100644 --- a/radicle-crdt/src/lwwmap.rs +++ b/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 } } diff --git a/radicle-crdt/src/lwwreg.rs b/radicle-crdt/src/lwwreg.rs index 7f6dd67e..78a4eff2 100644 --- a/radicle-crdt/src/lwwreg.rs +++ b/radicle-crdt/src/lwwreg.rs @@ -33,10 +33,6 @@ impl LWWReg { let (t, c) = self.inner.into_inner(); (c, t) } - - pub fn merge(&mut self, other: Self) { - self.inner.merge(other.inner); - } } impl Semilattice for LWWReg @@ -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); } } diff --git a/radicle-crdt/src/lwwset.rs b/radicle-crdt/src/lwwset.rs index 1a722ad3..adc6bd97 100644 --- a/radicle-crdt/src/lwwset.rs +++ b/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); } } diff --git a/radicle-crdt/src/ord.rs b/radicle-crdt/src/ord.rs index ed254724..a3109d4a 100644 --- a/radicle-crdt/src/ord.rs +++ b/radicle-crdt/src/ord.rs @@ -57,11 +57,9 @@ impl From for Max { } impl Semilattice for Max { - fn join(self, other: Self) -> Self { + fn merge(&mut self, other: Self) { if other.0 > self.0 { - other - } else { - self + self.0 = other.0; } } } diff --git a/radicle-crdt/src/redactable.rs b/radicle-crdt/src/redactable.rs index 73ffbd62..de404717 100644 --- a/radicle-crdt/src/redactable.rs +++ b/radicle-crdt/src/redactable.rs @@ -19,8 +19,8 @@ impl From> for Redactable { } } -impl Redactable { - pub fn merge(&mut self, other: Self) { +impl Semilattice for Redactable { + fn merge(&mut self, other: Self) { match (&self, other) { (Self::Redacted, _) => {} (Self::Present(_), Self::Redacted) => { @@ -35,13 +35,6 @@ impl Redactable { } } -impl Semilattice for Redactable { - fn join(mut self, other: Self) -> Self { - self.merge(other); - self - } -} - #[cfg(test)] mod test { use quickcheck_macros::quickcheck;