From 41309538fb4d43fd07ed3ff87e94d462099eba15 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Sun, 27 Nov 2022 12:58:10 +0100 Subject: [PATCH] crdt: Small improvements and function additions Signed-off-by: Alexis Sellier --- radicle-crdt/src/lwwmap.rs | 16 ++++++++++++++++ radicle-crdt/src/lwwreg.rs | 3 ++- radicle-crdt/src/lwwset.rs | 7 ++++++- radicle-crdt/src/redactable.rs | 27 +++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/radicle-crdt/src/lwwmap.rs b/radicle-crdt/src/lwwmap.rs index 5aa61baa..84ed9d15 100644 --- a/radicle-crdt/src/lwwmap.rs +++ b/radicle-crdt/src/lwwmap.rs @@ -60,6 +60,10 @@ impl LWWMap bool { + self.iter().next().is_none() + } } impl Default for LWWMap { @@ -170,6 +174,18 @@ mod tests { assert!(!map.iter().any(|(c, _)| *c == 'a')); } + #[test] + fn test_is_empty() { + let mut map = LWWMap::default(); + assert!(map.is_empty()); + + map.insert('a', Max::from("alice"), 1); + assert!(!map.is_empty()); + + map.remove('a', 2); + assert!(map.is_empty()); + } + #[test] fn test_remove_insert() { let mut map = LWWMap::default(); diff --git a/radicle-crdt/src/lwwreg.rs b/radicle-crdt/src/lwwreg.rs index ffdc39e2..238a9260 100644 --- a/radicle-crdt/src/lwwreg.rs +++ b/radicle-crdt/src/lwwreg.rs @@ -1,5 +1,6 @@ use num_traits::Bounded; +use crate::clock; use crate::ord::Max; use crate::Semilattice; @@ -7,7 +8,7 @@ use crate::Semilattice; /// /// In case of conflict, uses the [`Semilattice`] instance of `T` to merge. #[derive(Debug, Clone, PartialEq, Eq)] -pub struct LWWReg { +pub struct LWWReg { clock: Max, value: T, } diff --git a/radicle-crdt/src/lwwset.rs b/radicle-crdt/src/lwwset.rs index 68f9478a..c198b8d1 100644 --- a/radicle-crdt/src/lwwset.rs +++ b/radicle-crdt/src/lwwset.rs @@ -1,8 +1,9 @@ +use crate::clock; use crate::{lwwmap::LWWMap, Semilattice}; /// Last-Write-Wins Set. #[derive(Debug, Clone, PartialEq, Eq)] -pub struct LWWSet { +pub struct LWWSet { inner: LWWMap, } @@ -28,6 +29,10 @@ impl LWWSet { pub fn iter(&self) -> impl Iterator { self.inner.iter().map(|(k, _)| k) } + + pub fn is_empty(&self) -> bool { + self.inner.is_empty() + } } impl Default for LWWSet { diff --git a/radicle-crdt/src/redactable.rs b/radicle-crdt/src/redactable.rs index 55e0f0cf..326ad331 100644 --- a/radicle-crdt/src/redactable.rs +++ b/radicle-crdt/src/redactable.rs @@ -18,6 +18,15 @@ pub enum Redactable { Redacted, } +impl Redactable { + pub fn get(&self) -> Option<&T> { + match self { + Self::Present(val) => Some(val), + Self::Redacted => None, + } + } +} + impl From> for Redactable { fn from(option: Option) -> Self { match option { @@ -27,6 +36,24 @@ impl From> for Redactable { } } +impl From> for Option { + fn from(redactable: Redactable) -> Self { + match redactable { + Redactable::Present(v) => Some(v), + Redactable::Redacted => None, + } + } +} + +impl<'a, T> From<&'a Redactable> for Option<&'a T> { + fn from(redactable: &'a Redactable) -> Self { + match redactable { + Redactable::Present(v) => Some(v), + Redactable::Redacted => None, + } + } +} + impl Semilattice for Redactable { fn merge(&mut self, other: Self) { match (&self, other) {