Skip to main content

oxigdal_sync/crdt/
mod.rs

1//! Conflict-free Replicated Data Types (CRDTs)
2//!
3//! This module provides various CRDT implementations for distributed
4//! state synchronization:
5//!
6//! - **LWW-Register**: Last-Write-Wins Register for single values
7//! - **G-Counter**: Grow-only Counter
8//! - **PN-Counter**: Positive-Negative Counter (increment/decrement)
9//! - **OR-Set**: Observed-Remove Set for add/remove operations
10//!
11//! CRDTs guarantee eventual consistency without requiring coordination
12//! between replicas.
13
14pub mod g_counter;
15pub mod lww_register;
16pub mod or_set;
17pub mod pn_counter;
18
19pub use g_counter::GCounter;
20pub use lww_register::LwwRegister;
21pub use or_set::OrSet;
22pub use pn_counter::PnCounter;
23
24use crate::{DeviceId, SyncResult};
25use serde::{Deserialize, Serialize};
26
27/// Trait for mergeable CRDTs
28///
29/// All CRDTs must implement this trait to allow merging of
30/// concurrent states.
31pub trait Crdt: Clone + Serialize + for<'de> Deserialize<'de> {
32    /// Merges another CRDT state into this one
33    ///
34    /// After merging, this CRDT should reflect the combined state
35    /// of both CRDTs in a way that guarantees convergence.
36    ///
37    /// # Arguments
38    ///
39    /// * `other` - The CRDT to merge with
40    fn merge(&mut self, other: &Self) -> SyncResult<()>;
41
42    /// Checks if this CRDT is causally dominated by another
43    ///
44    /// Returns true if all changes in this CRDT are also present
45    /// in the other CRDT.
46    fn dominated_by(&self, other: &Self) -> bool;
47}
48
49/// Trait for CRDTs that track device state
50pub trait DeviceAware {
51    /// Gets the device ID associated with this CRDT
52    fn device_id(&self) -> &DeviceId;
53
54    /// Sets the device ID
55    fn set_device_id(&mut self, device_id: DeviceId);
56}