Struct crdts::lwwreg::LWWReg

source ·
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>

source

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 });
source

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: Clone, M: Clone> Clone for LWWReg<V, M>

source§

fn clone(&self) -> LWWReg<V, M>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<V: PartialEq, M: Ord> CmRDT for LWWReg<V, M>

§

type Op = LWWReg<V, M>

Op defines a mutation to the CRDT. As long as Op’s from one actor are replayed in exactly the same order they were generated by that actor, the CRDT will converge. In other words, we must have a total ordering on each actors operations, while requiring only a partial order over all ops. E.g. Read more
§

type Validation = Validation

The validation error returned by validate_op.
source§

fn validate_op(&self, op: &Self::Op) -> Result<(), Self::Validation>

Some CRDT’s have stricter requirements on how they must be used. To avoid violating these requirements, CRDT’s provide an interface to optionally validate op’s before they are applied. Read more
source§

fn apply(&mut self, op: Self::Op)

Apply an Op to the CRDT
source§

impl<V: PartialEq, M: Ord> CvRDT for LWWReg<V, M>

source§

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)

Combines two LWWReg instances according to the marker that tracks causality.

§

type Validation = Validation

The validation error returned by validate_merge.
source§

impl<V: Debug, M: Debug> Debug for LWWReg<V, M>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<V: Default, M: Default> Default for LWWReg<V, M>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<'de, V, M> Deserialize<'de> for LWWReg<V, M>where V: Deserialize<'de>, M: Deserialize<'de>,

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<V: Hash, M: Hash> Hash for LWWReg<V, M>

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<V: PartialEq, M: PartialEq> PartialEq<LWWReg<V, M>> for LWWReg<V, M>

source§

fn eq(&self, other: &LWWReg<V, M>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<V, M> Serialize for LWWReg<V, M>where V: Serialize, M: Serialize,

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<V: Eq, M: Eq> Eq for LWWReg<V, M>

source§

impl<V, M> StructuralEq for LWWReg<V, M>

source§

impl<V, M> StructuralPartialEq for LWWReg<V, M>

Auto Trait Implementations§

§

impl<V, M> RefUnwindSafe for LWWReg<V, M>where M: RefUnwindSafe, V: RefUnwindSafe,

§

impl<V, M> Send for LWWReg<V, M>where M: Send, V: Send,

§

impl<V, M> Sync for LWWReg<V, M>where M: Sync, V: Sync,

§

impl<V, M> Unpin for LWWReg<V, M>where M: Unpin, V: Unpin,

§

impl<V, M> UnwindSafe for LWWReg<V, M>where M: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,