crdt: Make some improvements to the types
Signed-off-by: Alexis Sellier <alexis@radicle.xyz>
This commit is contained in:
parent
cc33d04a7b
commit
36ccab2d7a
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
|
|
|
|||
Loading…
Reference in New Issue