pub struct LWWReg<V, M> {
pub val: V,
pub marker: M,
}
Expand description
LWWReg
is a simple CRDT that contains an arbitrary value
along with an Ord
that tracks causality. It is the responsibility
of the user to guarantee that the source of the causal element
is monotonic. Don’t use timestamps unless you are comfortable
with divergence.
M
is a marker. It must grow monotonically and must be globally unique
Fields§
§val: V
val
is the opaque element contained within this CRDT
marker: M
marker
should be a monotonic value associated with this val
Implementations§
source§impl<V: PartialEq, M: Ord> LWWReg<V, M>
impl<V: PartialEq, M: Ord> LWWReg<V, M>
sourcepub fn update(&mut self, val: V, marker: M)
pub fn update(&mut self, val: V, marker: M)
Updates value witnessed by the given marker.
use crdts::LWWReg;
let mut reg = LWWReg { val: 1, marker: 2 };
// updating with a smaller marker is a no-op
reg.update(2, 1);
assert_eq!(reg.val, 1);
// updating with larger marker succeeds
reg.update(2, 3);
assert_eq!(reg, LWWReg { val: 2, marker: 3 });
sourcepub fn validate_update(&self, val: &V, marker: &M) -> Result<(), Validation>
pub fn validate_update(&self, val: &V, marker: &M) -> Result<(), Validation>
An update is invalid if the marker is exactly the same as the current marker BUT the value is different:
use crdts::{lwwreg, LWWReg};
let mut reg = LWWReg { val: 1, marker: 2 };
// updating with a smaller marker is a no-op
assert_eq!(reg.validate_update(&32, &2), Err(lwwreg::Validation::ConflictingMarker));
Trait Implementations§
source§impl<V: PartialEq, M: Ord> CmRDT for LWWReg<V, M>
impl<V: PartialEq, M: Ord> CmRDT for LWWReg<V, M>
§type Op = LWWReg<V, M>
type Op = LWWReg<V, M>
§type Validation = Validation
type Validation = Validation
validate_op
.source§fn validate_op(&self, op: &Self::Op) -> Result<(), Self::Validation>
fn validate_op(&self, op: &Self::Op) -> Result<(), Self::Validation>
source§impl<V: PartialEq, M: Ord> CvRDT for LWWReg<V, M>
impl<V: PartialEq, M: Ord> CvRDT for LWWReg<V, M>
source§fn validate_merge(&self, other: &Self) -> Result<(), Self::Validation>
fn validate_merge(&self, other: &Self) -> Result<(), Self::Validation>
Validates whether a merge is safe to perfom
Returns an error if the marker is identical but the contained element is different.
use crdts::{lwwreg, LWWReg, CvRDT};
let mut l1 = LWWReg { val: 1, marker: 2 };
let l2 = LWWReg { val: 3, marker: 2 };
// errors!
assert_eq!(l1.validate_merge(&l2), Err(lwwreg::Validation::ConflictingMarker));
source§fn merge(&mut self, _: Self)
fn merge(&mut self, _: Self)
Combines two LWWReg
instances according to the marker that
tracks causality.
§type Validation = Validation
type Validation = Validation
validate_merge
.