Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
crdt: Small improvements and function additions
Alexis Sellier committed 3 years ago
commit 41309538fb4d43fd07ed3ff87e94d462099eba15
parent be789a487fe16a0061aa5bccdcd4465aeed74c08
4 files changed +51 -2
modified radicle-crdt/src/lwwmap.rs
@@ -60,6 +60,10 @@ impl<K: Ord, V: Semilattice + PartialOrd + Eq, C: PartialOrd + Ord> LWWMap<K, V,
            .iter()
            .filter_map(|(k, v)| v.get().as_ref().map(|v| (k, v)))
    }
+

+
    pub fn is_empty(&self) -> bool {
+
        self.iter().next().is_none()
+
    }
}

impl<K, V, C> Default for LWWMap<K, V, C> {
@@ -171,6 +175,18 @@ mod tests {
    }

    #[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();

modified 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<T, C> {
+
pub struct LWWReg<T, C = clock::Lamport> {
    clock: Max<C>,
    value: T,
}
modified 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<T, C> {
+
pub struct LWWSet<T, C = clock::Lamport> {
    inner: LWWMap<T, (), C>,
}

@@ -28,6 +29,10 @@ impl<T: Ord, C: Ord> LWWSet<T, C> {
    pub fn iter(&self) -> impl Iterator<Item = &T> {
        self.inner.iter().map(|(k, _)| k)
    }
+

+
    pub fn is_empty(&self) -> bool {
+
        self.inner.is_empty()
+
    }
}

impl<T, C> Default for LWWSet<T, C> {
modified radicle-crdt/src/redactable.rs
@@ -18,6 +18,15 @@ pub enum Redactable<T> {
    Redacted,
}

+
impl<T> Redactable<T> {
+
    pub fn get(&self) -> Option<&T> {
+
        match self {
+
            Self::Present(val) => Some(val),
+
            Self::Redacted => None,
+
        }
+
    }
+
}
+

impl<T> From<Option<T>> for Redactable<T> {
    fn from(option: Option<T>) -> Self {
        match option {
@@ -27,6 +36,24 @@ impl<T> From<Option<T>> for Redactable<T> {
    }
}

+
impl<T> From<Redactable<T>> for Option<T> {
+
    fn from(redactable: Redactable<T>) -> Self {
+
        match redactable {
+
            Redactable::Present(v) => Some(v),
+
            Redactable::Redacted => None,
+
        }
+
    }
+
}
+

+
impl<'a, T> From<&'a Redactable<T>> for Option<&'a T> {
+
    fn from(redactable: &'a Redactable<T>) -> Self {
+
        match redactable {
+
            Redactable::Present(v) => Some(v),
+
            Redactable::Redacted => None,
+
        }
+
    }
+
}
+

impl<T: PartialEq> Semilattice for Redactable<T> {
    fn merge(&mut self, other: Self) {
        match (&self, other) {