Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
crdt: Make some improvements to the types
Alexis Sellier committed 3 years ago
commit 36ccab2d7a589d926c5c8711ea3428cfcf1e8cf3
parent cc33d04a7ba8ad181ca42b7ab307b8159df883de
2 files changed +27 -3
modified radicle-crdt/src/change.rs
@@ -25,12 +25,14 @@ pub struct Change<A> {
    pub clock: LClock,
}

-
impl<'de, A: Serialize + Deserialize<'de>> Change<A> {
+
impl<A> Change<A> {
    /// Get the change id.
    pub fn id(&self) -> ChangeId {
        (self.clock, self.author)
    }
+
}

+
impl<A: Serialize> Change<A> {
    /// Serialize the change into a byte string.
    pub fn encode(&self) -> Vec<u8> {
        let mut buf = Vec::new();
@@ -41,7 +43,9 @@ impl<'de, A: Serialize + Deserialize<'de>> Change<A> {

        buf
    }
+
}

+
impl<'de, A: Deserialize<'de>> Change<A> {
    /// Deserialize a change from a byte string.
    pub fn decode(bytes: &'de [u8]) -> Result<Self, serde_json::Error> {
        serde_json::from_slice(bytes)
modified radicle-crdt/src/lwwreg.rs
@@ -1,3 +1,5 @@
+
use num_traits::Bounded;
+

use crate::ord::Max;
use crate::Semilattice;

@@ -43,10 +45,28 @@ impl<T: PartialOrd + Semilattice, C: PartialOrd> LWWReg<T, C> {
    }
}

+
impl<T, C: Default> From<T> for LWWReg<T, C> {
+
    fn from(value: T) -> Self {
+
        Self {
+
            clock: Max::from(C::default()),
+
            value,
+
        }
+
    }
+
}
+

+
impl<T: Default, C: Default + Bounded> Default for LWWReg<T, C> {
+
    fn default() -> Self {
+
        Self {
+
            clock: Max::default(),
+
            value: T::default(),
+
        }
+
    }
+
}
+

impl<T, C> Semilattice for LWWReg<T, C>
where
-
    T: PartialOrd + Default + Semilattice,
-
    C: PartialOrd + Default,
+
    T: PartialOrd + Semilattice,
+
    C: PartialOrd,
{
    fn merge(&mut self, other: Self) {
        self.set(other.value, other.clock.into_inner());