use ossa_typeable::Typeable;
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use crate::{
time::{compare_with_tiebreak, CausalState},
CRDT,
};
#[derive(Clone, Debug, PartialEq, Typeable, Serialize, Deserialize)]
pub struct LWW<T, A> {
pub time: T,
pub value: A,
}
impl<T, A> LWW<T, A> {
pub fn new(time: T, value: A) -> Self {
LWW { time, value }
}
pub fn time(&self) -> &T {
&self.time
}
pub fn value(&self) -> &A {
&self.value
}
}
impl<T: Ord, A> CRDT for LWW<T, A> {
type Op = LWW<T, A>;
type Time = T;
fn apply<CS: CausalState<Time = Self::Time>>(self, st: &CS, op: Self::Op) -> Self {
match compare_with_tiebreak(st, &self.time, &op.time) {
Ordering::Less => op,
Ordering::Greater => self,
Ordering::Equal => unreachable!(
"Precondition of `apply` violated: Applied `logical_time`s must be unique."
),
}
}
}