Skip to main content

atomr_distributed_data/
traits.rs

1/// Convergent replicated data type merge semantics.
2///
3/// Mirrors Akka.DistributedData's `IReplicatedData.Merge`.
4///
5/// **Sealed** as part of Phase 13 (idiomatic-rust sweep): only the
6/// CRDTs in this crate may implement `CrdtMerge` directly. Downstream
7/// users compose CRDTs via `ORMap<K, V>` / `LWWMap<K, V>` (where the
8/// value is a CRDT) or wrap them in domain-specific structs.
9pub trait CrdtMerge: Clone + private::Sealed {
10    fn merge(&mut self, other: &Self);
11}
12
13/// Optional delta-CRDT layer: emit a small "delta" describing the
14/// last local change and merge incoming deltas into the full state.
15///
16/// Phase 8.C of `docs/full-port-plan.md`. CRDTs that implement
17/// [`DeltaCrdt`] participate in delta-gossip — the Replicator ships
18/// `Self::Delta` to peers instead of the full state, dramatically
19/// reducing wire traffic for hot keys.
20///
21/// CRDTs whose state is small (counters, flags) can implement this
22/// trivially with `Delta = Self`. Sets / maps emit a sub-state
23/// containing only the keys / tags that changed.
24pub trait DeltaCrdt: CrdtMerge {
25    /// Per-CRDT delta type — typically `Self` for small CRDTs, a
26    /// sub-state for set / map shapes.
27    type Delta: Clone + Send + 'static;
28
29    /// Take and clear the accumulated local delta. Returns `None` if
30    /// no local change has happened since the last call.
31    fn take_delta(&mut self) -> Option<Self::Delta>;
32
33    /// Merge an incoming delta into local state.
34    fn merge_delta(&mut self, delta: &Self::Delta);
35}
36
37mod private {
38    pub trait Sealed {}
39
40    impl Sealed for crate::counters::GCounter {}
41    impl Sealed for crate::counters::PNCounter {}
42    impl Sealed for crate::flag::Flag {}
43    impl<T: Eq + std::hash::Hash + Clone> Sealed for crate::sets::GSet<T> {}
44    impl<T: Eq + std::hash::Hash + Clone> Sealed for crate::sets::OrSet<T> {}
45    impl<T: Clone> Sealed for crate::register::LwwRegister<T> {}
46    impl<K, V> Sealed for crate::maps::ORMap<K, V>
47    where
48        K: Eq + std::hash::Hash + Clone,
49        V: super::CrdtMerge,
50    {
51    }
52    impl<K, V> Sealed for crate::maps::LWWMap<K, V>
53    where
54        K: Eq + std::hash::Hash + Clone,
55        V: Clone,
56    {
57    }
58    impl<K> Sealed for crate::maps::PNCounterMap<K> where K: Eq + std::hash::Hash + Clone {}
59    impl<K, V> Sealed for crate::maps::ORMultiMap<K, V>
60    where
61        K: Eq + std::hash::Hash + Clone,
62        V: Eq + std::hash::Hash + Clone,
63    {
64    }
65}