Skip to main content

bones_core/crdt/
merge.rs

1use crate::crdt::{EpochPhase, GSet, Lww, Timestamp};
2use std::hash::Hash;
3use tracing::instrument;
4
5pub trait Merge {
6    fn merge(&mut self, other: Self);
7}
8
9impl Merge for Timestamp {
10    #[instrument(skip(self))]
11    fn merge(&mut self, other: Self) {
12        if other > *self {
13            *self = other;
14        }
15    }
16}
17
18impl<T> Merge for Lww<T> {
19    fn merge(&mut self, other: Self) {
20        if other.timestamp > self.timestamp {
21            *self = other;
22        }
23    }
24}
25
26impl<T: Eq + Hash + Clone> Merge for GSet<T> {
27    fn merge(&mut self, other: Self) {
28        for element in other.elements {
29            self.elements.insert(element);
30        }
31    }
32}
33
34// NOTE: OrSet Merge impl is in orset.rs (proper add-wins OR-Set).
35
36impl Merge for EpochPhase {
37    fn merge(&mut self, other: Self) {
38        if other.epoch > self.epoch {
39            *self = other;
40        } else if other.epoch == self.epoch && other.phase > self.phase {
41            self.phase = other.phase;
42        }
43    }
44}