Skip to main content

crdt_kit/
lib.rs

1//! # crdt-kit
2//!
3//! CRDTs optimized for edge computing and local-first applications.
4//!
5//! A CRDT (Conflict-free Replicated Data Type) is a data structure that can be
6//! replicated across multiple devices and updated independently. When replicas
7//! are merged, they are guaranteed to converge to the same state without
8//! requiring coordination or consensus.
9//!
10//! ## `no_std` Support
11//!
12//! This crate supports `no_std` environments with the `alloc` crate.
13//! Disable the default `std` feature in your `Cargo.toml`:
14//!
15//! ```toml
16//! [dependencies]
17//! crdt-kit = { version = "0.5.0", default-features = false }
18//! ```
19//!
20//! ## Quick Start
21//!
22//! ```
23//! use crdt_kit::prelude::*;
24//!
25//! // Grow-only counter
26//! let mut c1 = GCounter::new(1);
27//! c1.increment();
28//!
29//! let mut c2 = GCounter::new(2);
30//! c2.increment();
31//!
32//! c1.merge(&c2);
33//! assert_eq!(c1.value(), 2);
34//! ```
35//!
36//! ## Available CRDTs
37//!
38//! ### Counters
39//! - [`GCounter`] - Grow-only counter (increment only)
40//! - [`PNCounter`] - Positive-negative counter (increment and decrement)
41//!
42//! ### Registers
43//! - [`LWWRegister`] - Last-writer-wins register (HLC-based resolution)
44//! - [`MVRegister`] - Multi-value register (preserves concurrent writes)
45//!
46//! ### Sets
47//! - [`GSet`] - Grow-only set (add only)
48//! - [`TwoPSet`] - Two-phase set (add and remove, remove is permanent)
49//! - [`ORSet`] - Observed-remove set (add and remove freely)
50//!
51//! ### Maps
52//! - [`LWWMap`] - Last-writer-wins map (per-key HLC timestamp resolution)
53//! - [`AWMap`] - Add-wins map (OR-Set semantics for keys, concurrent add beats remove)
54//!
55//! ### Sequences
56//! - [`Rga`] - Replicated Growable Array (ordered sequence)
57//! - [`TextCrdt`] - Collaborative text (thin wrapper over `Rga<char>`)
58//!
59//! ## The `Crdt` Trait
60//!
61//! All types implement the [`Crdt`] trait, which provides the [`Crdt::merge`]
62//! method. Merge is guaranteed to be commutative, associative, and idempotent.
63
64#![cfg_attr(not(feature = "std"), no_std)]
65#![warn(missing_docs)]
66
67extern crate alloc;
68
69mod aw_map;
70mod crdt;
71mod gcounter;
72mod gset;
73mod lww_map;
74mod lww_register;
75mod mv_register;
76mod or_set;
77mod pncounter;
78/// Replicated Growable Array (RGA) — ordered sequence CRDT.
79pub mod rga;
80mod text;
81mod twop_set;
82/// Versioned serialization and envelope format.
83pub mod version;
84#[cfg(feature = "wasm")]
85mod wasm;
86
87pub mod clock;
88pub mod prelude;
89
90pub use aw_map::{AWMap, AWMapDelta};
91pub use crdt::{Crdt, DeltaCrdt, NodeId};
92pub use gcounter::{GCounter, GCounterDelta};
93pub use lww_map::{LWWMap, LWWMapDelta};
94pub use gset::{GSet, GSetDelta};
95pub use lww_register::{LWWRegister, LWWRegisterDelta};
96pub use mv_register::{MVRegister, MVRegisterDelta};
97pub use or_set::{ORSet, ORSetDelta};
98pub use pncounter::{PNCounter, PNCounterDelta};
99pub use rga::{Rga, RgaDelta, RgaError, RgaNode};
100pub use text::{TextCrdt, TextDelta, TextError};
101pub use twop_set::{TwoPSet, TwoPSetDelta};
102pub use version::{
103    CrdtType, EnvelopeError, VersionError, Versioned, VersionedEnvelope, ENVELOPE_HEADER_SIZE,
104    MAGIC_BYTE,
105};