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.2", default-features = false }
18//! ```
19//!
20//! Note: [`LWWRegister::new`] and [`LWWRegister::set`] require the `std`
21//! feature for automatic timestamps via `SystemTime`.
22//!
23//! ## Quick Start
24//!
25//! ```
26//! use crdt_kit::prelude::*;
27//!
28//! // Grow-only counter
29//! let mut c1 = GCounter::new("device-1");
30//! c1.increment();
31//!
32//! let mut c2 = GCounter::new("device-2");
33//! c2.increment();
34//!
35//! c1.merge(&c2);
36//! assert_eq!(c1.value(), 2);
37//! ```
38//!
39//! ## Available CRDTs
40//!
41//! ### Counters
42//! - [`GCounter`] - Grow-only counter (increment only)
43//! - [`PNCounter`] - Positive-negative counter (increment and decrement)
44//!
45//! ### Registers
46//! - [`LWWRegister`] - Last-writer-wins register (timestamp-based resolution)
47//! - [`MVRegister`] - Multi-value register (preserves concurrent writes)
48//!
49//! ### Sets
50//! - [`GSet`] - Grow-only set (add only)
51//! - [`TwoPSet`] - Two-phase set (add and remove, remove is permanent)
52//! - [`ORSet`] - Observed-remove set (add and remove freely)
53//!
54//! ## The `Crdt` Trait
55//!
56//! All types implement the [`Crdt`] trait, which provides the [`Crdt::merge`]
57//! method. Merge is guaranteed to be commutative, associative, and idempotent.
58
59#![cfg_attr(not(feature = "std"), no_std)]
60
61extern crate alloc;
62
63mod crdt;
64mod gcounter;
65mod gset;
66mod lww_register;
67mod mv_register;
68mod or_set;
69mod pncounter;
70mod rga;
71mod text;
72mod twop_set;
73#[cfg(feature = "wasm")]
74mod wasm;
75
76pub mod prelude;
77
78pub use crdt::{Crdt, DeltaCrdt};
79pub use gcounter::{GCounter, GCounterDelta};
80pub use gset::GSet;
81pub use lww_register::LWWRegister;
82pub use mv_register::MVRegister;
83pub use or_set::{ORSet, ORSetDelta};
84pub use pncounter::{PNCounter, PNCounterDelta};
85pub use rga::Rga;
86pub use text::TextCrdt;
87pub use twop_set::TwoPSet;