extern crate crdts;
use crdts::{CmRDT, CvRDT, VClock};
use std::cmp::Ordering::*;
fn main() {
#[derive(Debug, Default, Clone, PartialEq)]
struct VersionedString {
clock: VClock<String>,
data: String,
}
let shared_password = VersionedString::default();
let mut bobs_copy = shared_password.clone();
let mut alices_copy = shared_password;
bobs_copy
.clock
.apply(bobs_copy.clock.inc("BOB".to_string()));
bobs_copy.data = "pa$$w0rd".to_string();
match alices_copy.clock.partial_cmp(&bobs_copy.clock) {
Some(Less) => { }
_ => panic!("Bob's clock should be ahead!!"),
}
alices_copy = bobs_copy.clone();
alices_copy
.clock
.apply(alices_copy.clock.inc("ALICE".to_string()));
alices_copy.data = "letMein32".to_string();
bobs_copy
.clock
.apply(bobs_copy.clock.inc("BOB".to_string()));
bobs_copy.data = "0sdjf0as9j13k0zc".to_string();
match bobs_copy.clock.partial_cmp(&alices_copy.clock) {
None => { }
_ => panic!("These clocks are not ordered!"),
}
assert_eq!(format!("{}", bobs_copy.clock), "<BOB:2>");
assert_eq!(format!("{}", alices_copy.clock), "<ALICE:1, BOB:1>");
bobs_copy.clock.merge(alices_copy.clock.clone());
bobs_copy.data = "letMein32".to_string();
assert_eq!(format!("{}", bobs_copy.clock), "<ALICE:1, BOB:2>");
match alices_copy.clock.partial_cmp(&bobs_copy.clock) {
Some(Less) => {
alices_copy = bobs_copy.clone()
}
_ => panic!("Alice's clock should be behind!!"),
}
assert_eq!(alices_copy, bobs_copy);
}