pub struct LWWRegister<T: Clone> { /* private fields */ }Expand description
A last-writer-wins register (LWW-Register).
Resolves concurrent writes by keeping the value with the highest timestamp. Ties are broken by comparing actor IDs lexicographically.
§Example
use crdt_kit::prelude::*;
let mut r1 = LWWRegister::new("node-1", "hello");
let mut r2 = LWWRegister::new("node-2", "world");
// The register with the later timestamp wins
r1.merge(&r2);
// Value is either "hello" or "world" depending on timestampsImplementations§
Source§impl<T: Clone> LWWRegister<T>
impl<T: Clone> LWWRegister<T>
Sourcepub fn new(actor: impl Into<String>, value: T) -> Self
pub fn new(actor: impl Into<String>, value: T) -> Self
Create a new LWW-Register with an initial value.
The timestamp is automatically set to the current system time.
This method requires the std feature. In no_std environments, use
LWWRegister::with_timestamp instead.
Sourcepub fn with_timestamp(
actor: impl Into<String>,
value: T,
timestamp: u64,
) -> Self
pub fn with_timestamp( actor: impl Into<String>, value: T, timestamp: u64, ) -> Self
Create a new LWW-Register with an explicit timestamp.
Useful for testing or when you need deterministic behavior.
This is the only constructor available in no_std environments.
Sourcepub fn set(&mut self, value: T)
pub fn set(&mut self, value: T)
Update the register’s value.
The timestamp is automatically set to the current system time.
This method requires the std feature. In no_std environments, use
LWWRegister::set_with_timestamp instead.
Sourcepub fn set_with_timestamp(&mut self, value: T, timestamp: u64)
pub fn set_with_timestamp(&mut self, value: T, timestamp: u64)
Update the register’s value with an explicit timestamp.
Trait Implementations§
Source§impl<T: Clone + Clone> Clone for LWWRegister<T>
impl<T: Clone + Clone> Clone for LWWRegister<T>
Source§fn clone(&self) -> LWWRegister<T>
fn clone(&self) -> LWWRegister<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more