1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/// Compact node identifier for CRDT replicas.
///
/// Uses `u64` instead of `String` to avoid heap allocations on every
/// operation — critical for embedded/IoT targets where `alloc` is expensive.
///
/// # Example
///
/// ```
/// use crdt_kit::prelude::*;
///
/// let mut c = GCounter::new(1); // NodeId = 1
/// c.increment();
/// assert_eq!(c.value(), 1);
/// ```
pub type NodeId = u64;
/// Core trait that all CRDTs must implement.
///
/// A CRDT (Conflict-free Replicated Data Type) guarantees that concurrent
/// updates on different replicas will converge to the same state after merging,
/// without requiring coordination.
///
/// # Properties
///
/// All implementations must satisfy:
/// - **Commutativity:** `a.merge(b) == b.merge(a)`
/// - **Associativity:** `a.merge(b.merge(c)) == a.merge(b).merge(c)`
/// - **Idempotency:** `a.merge(a) == a`
/// Extension trait for delta-state CRDTs.
///
/// Delta-state CRDTs can produce compact deltas representing only the
/// changes between two states. This enables efficient synchronization:
/// instead of transferring the full state, replicas exchange small deltas.
///
/// # Example
///
/// ```
/// use crdt_kit::prelude::*;
///
/// let mut c1 = GCounter::new(1);
/// c1.increment();
/// c1.increment();
///
/// let mut c2 = GCounter::new(2);
/// c2.increment();
///
/// // Generate a delta from c1 that c2 doesn't have
/// let delta = c1.delta(&c2);
///
/// // Apply just the delta instead of full state merge
/// c2.apply_delta(&delta);
/// assert_eq!(c2.value(), 3); // both counts included
/// ```