use std::fmt::Debug;
use serde::{Deserialize, Serialize};
use crate::{CmRDT, Dot, VClock};
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ReadCtx<V, A: Ord> {
pub add_clock: VClock<A>,
pub rm_clock: VClock<A>,
pub val: V,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct AddCtx<A: Ord> {
pub clock: VClock<A>,
pub dot: Dot<A>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RmCtx<A: Ord> {
pub clock: VClock<A>,
}
impl<V, A: Ord + Clone + Debug> ReadCtx<V, A> {
pub fn derive_add_ctx(self, actor: A) -> AddCtx<A> {
let mut clock = self.add_clock;
let dot = clock.inc(actor);
clock.apply(dot.clone());
AddCtx { clock, dot }
}
pub fn derive_rm_ctx(self) -> RmCtx<A> {
RmCtx {
clock: self.rm_clock,
}
}
pub fn split(self) -> (V, ReadCtx<(), A>) {
(
self.val,
ReadCtx {
add_clock: self.add_clock,
rm_clock: self.rm_clock,
val: (),
},
)
}
}