use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
pub struct LamportClock {
id: String,
time: u64,
}
impl LamportClock {
pub fn new(id: &str) -> LamportClock {
LamportClock {
id: id.to_owned(),
time: 0,
}
}
pub fn set_time(mut self, time: u64) -> LamportClock {
self.time = time;
self
}
pub fn time(&self) -> u64 {
self.time
}
pub fn id(&self) -> &str {
&self.id
}
pub fn tick(&mut self) {
self.time += 1;
}
pub fn merge(&mut self, o: &LamportClock) {
if self.time < o.time {
self.time = o.time;
}
}
}
impl PartialEq for LamportClock {
fn eq(&self, other: &Self) -> bool {
self.time == other.time && self.id == other.id
}
}
impl Eq for LamportClock {}
impl Ord for LamportClock {
fn cmp(&self, other: &Self) -> Ordering {
self.time
.cmp(&other.time)
.then_with(|| self.id.cmp(&other.id))
}
}
impl PartialOrd for LamportClock {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}