logicaffeine-data
WASM-safe data structures and CRDTs for distributed systems.
Part of the Logicaffeine project.
The Lamport Invariant
This crate enforces a strict boundary: no IO, no system time, no network access. It compiles cleanly for both native and wasm32-unknown-unknown targets.
All timestamps must be injected by callers. This means:
LWWRegisterrequires explicit timestamp parameters- Replica IDs are generated using
getrandom(works in WASM) - The
Synced<T>networking wrapper lives inlogicaffeine_system, not here
Features
- WASM-compatible: Compiles for native and WebAssembly targets
- Pure data structures: No tokio, no libp2p, no SystemTime dependencies
- Serializable: All types implement
serde::SerializeandDeserialize - Delta sync: Efficient incremental synchronization via
DeltaCrdttrait
CRDT Types
CRDTs (Conflict-free Replicated Data Types) provide automatic conflict resolution. Any two replicas can merge to produce the same result, regardless of message order.
Counters
| Type | Description | Use Case |
|---|---|---|
GCounter |
Grow-only counter | View counts, page hits |
PNCounter |
Positive-negative counter | Bidirectional counters (upvotes/downvotes) |
use ;
let mut counter = new;
counter.increment;
assert_eq!;
let mut pn = new;
pn.increment;
pn.decrement;
assert_eq!;
Registers
| Type | Description | Use Case |
|---|---|---|
LWWRegister<T> |
Last-write-wins | Single values where latest update should win |
MVRegister<T> |
Multi-value | Track conflicts for manual resolution |
use ;
// LWW: Timestamp determines winner
let mut reg = new;
reg.set; // Higher timestamp wins
// MV: Preserves concurrent writes for conflict detection
let mut mv: = new;
mv.set;
if mv.has_conflict
Sets
| Type | Description | Use Case |
|---|---|---|
ORSet<T, AddWins> |
Concurrent add beats remove | Collaborative collections (default) |
ORSet<T, RemoveWins> |
Concurrent remove beats add | Access revocation, cleanup operations |
use ;
let mut set: = new;
set.add;
assert!;
// With remove-wins bias
let mut strict: = new;
Maps
| Type | Description | Use Case |
|---|---|---|
ORMap<K, V> |
Key-value map with nested CRDTs | Structured data, nested counters/sets |
use ;
let mut scores: = new;
scores.get_or_insert.increment;
assert_eq!;
Sequences
| Type | Description | Use Case |
|---|---|---|
RGA |
Replicated Growable Array | Collaborative lists |
YATA |
Yet Another Text Algorithm | Collaborative text editing |
use ;
let mut list: = RGAnew;
list.append;
list.append;
assert_eq!;
let mut text: = YATAnew;
text.append;
text.append;
Causal Infrastructure
These types track causality and enable conflict detection.
| Type | Description |
|---|---|
VClock |
Vector clock for causal ordering |
Dot |
Unique event identifier (replica ID + sequence number) |
DotContext |
Combines clock + cloud for out-of-order message handling |
DeltaBuffer<D> |
Ring buffer for recent deltas (efficient sync) |
use ;
let mut clock = new;
let seq = clock.increment; // Returns sequence number
let dot = new;
let mut ctx = new;
let next_dot = ctx.next; // Generate and track
assert!;
Runtime Types
Type aliases for LOGOS programs:
| LOGOS Type | Rust Type | Description |
|---|---|---|
Nat |
u64 |
Natural numbers |
Int |
i64 |
Signed integers |
Real |
f64 |
Floating-point |
Text |
String |
UTF-8 strings |
Bool |
bool |
Boolean values |
Unit |
() |
Unit type |
Char |
char |
Unicode scalar |
Byte |
u8 |
Raw bytes |
Seq<T> |
Vec<T> |
Ordered sequences |
Set<T> |
HashSet<T> |
Unique elements |
Map<K,V> |
HashMap<K,V> |
Key-value pairs |
Tuple |
Vec<Value> |
Heterogeneous tuples |
Value |
enum | Dynamic type for mixed collections |
Key Traits
Merge
The core CRDT trait. Must satisfy:
- Commutative:
a.merge(b) == b.merge(a) - Associative:
a.merge(b.merge(c)) == a.merge(b).merge(c) - Idempotent:
a.merge(a) == a
use Merge;
// All CRDT types implement Merge
DeltaCrdt
For efficient incremental synchronization:
use ;
// Extract changes since a known version
LogosIndex / LogosIndexMut
1-based indexing for natural language conventions:
use ;
let v = vec!;
assert_eq!; // Index 1 = first element
assert_eq!; // Index 3 = third element
// Index 0 panics!
LogosContains
Unified containment testing:
use LogosContains;
let v = vec!;
assert!;
let s = Stringfrom;
assert!; // Substring
assert!; // Character
Usage Example
use ;
// Create state on replica 1
let mut replica1: = new;
replica1.get_or_insert.increment;
// Create state on replica 2
let mut replica2: = new;
replica2.get_or_insert.increment;
// Merge - order doesn't matter, result is always the same
replica1.merge;
assert_eq!;
Important Constraints
- No IO - Timestamps and network sync are the caller's responsibility
- 1-based indexing -
LogosIndexpanics on index 0 or out-of-bounds - For networking - Use
logicaffeine_system'sSynced<T>wrapper
License
Business Source License 1.1 (BUSL-1.1)
- Free for individuals and organizations with <25 employees
- Commercial license required for organizations with 25+ employees offering Logic Services
- Converts to MIT on December 24, 2029
See LICENSE for full terms.