pub struct Clock<A: Actor, E: EventSet> { /* private fields */ }Implementations§
Source§impl<A: Actor, E: EventSet> Clock<A, E>
impl<A: Actor, E: EventSet> Clock<A, E>
Sourcepub fn with<I: IntoIterator<Item = A>>(iter: I) -> Self
pub fn with<I: IntoIterator<Item = A>>(iter: I) -> Self
Returns a new Clock mapping each actor to a bottom entry.
§Examples
use std::collections::HashMap;
use std::iter::FromIterator;
use threshold::*;
let actors = vec!["A", "B"];
let vclock = VClock::with(actors);
assert_eq!(
vclock.frontier(),
VClock::from(vec![("A", MaxSet::from(0)), ("B", MaxSet::from(0))])
);Sourcepub fn from<I: IntoIterator<Item = (A, E)>>(iter: I) -> Self
pub fn from<I: IntoIterator<Item = (A, E)>>(iter: I) -> Self
Creates a Clock from an iterator of tuples (actor identifier and event
set).
§Examples
use threshold::*;
let a = ("A", MaxSet::from_event(10));
let b = ("B", MaxSet::from_event(20));
let vclock = Clock::from(vec![a, b]);
assert!(vclock.contains(&"A", 9));
assert!(!vclock.contains(&"A", 11));Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of actors in the clock.
§Examples
use threshold::*;
let a = ("A", MaxSet::from_event(10));
let b = ("B", MaxSet::from_event(20));
let vclock = Clock::from(vec![a, b]);
assert_eq!(vclock.len(), 2);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Checks that a clock is empty.
§Examples
use threshold::*;
let a = ("A", MaxSet::from_event(10));
let b = ("B", MaxSet::from_event(20));
let mut vclock = Clock::from(vec![a, b]);
assert!(!vclock.is_empty());
vclock = VClock::new();
assert!(vclock.is_empty());Sourcepub fn next(&mut self, actor: &A) -> u64
pub fn next(&mut self, actor: &A) -> u64
Returns the next event for the actor while updating its entry in the
clock.
§Examples
use threshold::*;
let actor_a = "A";
let actor_b = "B";
let mut clock = VClock::new();
let next = clock.next(&actor_a);
assert_eq!(next, 1);
let next = clock.next(&actor_a);
assert_eq!(next, 2);
let next = clock.next(&actor_a);
assert_eq!(next, 3);Sourcepub fn get(&self, actor: &A) -> Option<&E>
pub fn get(&self, actor: &A) -> Option<&E>
Retrieves the event set associated with some actor.
§Examples
use threshold::*;
let actor_a = "A";
let mut clock = VClock::new();
assert_eq!(clock.get(&actor_a), None);
clock.add(&actor_a, 1);
clock.add(&actor_a, 2);
let max_set = clock.get(&actor_a).expect("there should be an event set");
let mut iter = max_set.clone().event_iter();
assert_eq!(iter.next(), Some(1));
assert_eq!(iter.next(), Some(2));
assert_eq!(iter.next(), None);Sourcepub fn get_mut(&mut self, actor: &A) -> Option<&mut E>
pub fn get_mut(&mut self, actor: &A) -> Option<&mut E>
Retrieves (a mutable reference to) the event set associated with some
actor.
§Examples
use threshold::*;
let actor_a = "A";
let mut clock = VClock::new();
assert_eq!(clock.get_mut(&actor_a), None);
clock.add(&actor_a, 1);
clock.add(&actor_a, 2);
let max_set = clock
.get_mut(&actor_a)
.expect("there should be an event set");
max_set.add_event(3);
let mut iter = max_set.clone().event_iter();
assert_eq!(iter.next(), Some(1));
assert_eq!(iter.next(), Some(2));
assert_eq!(iter.next(), Some(3));
assert_eq!(iter.next(), None);Sourcepub fn add(&mut self, actor: &A, seq: u64) -> bool
pub fn add(&mut self, actor: &A, seq: u64) -> bool
Adds an event to the clock.
If the clock did not have this event present, true is returned.
If the clock did have this event present, false is returned.
§Examples
use threshold::*;
let actor_a = "A";
let actor_b = "B";
let mut clock = VClock::new();
assert!(!clock.contains(&actor_a, 1));
clock.add(&actor_a, 1);
assert!(clock.contains(&actor_a, 1));
assert!(!clock.contains(&actor_b, 1));
clock.add(&actor_b, 1);
assert!(clock.contains(&actor_b, 1));Sourcepub fn add_range(&mut self, actor: &A, start: u64, end: u64) -> bool
pub fn add_range(&mut self, actor: &A, start: u64, end: u64) -> bool
Adds a range of events to the clock.
§Examples
use threshold::*;
let actor_a = "A";
let actor_b = "B";
let mut clock_a = VClock::new();
clock_a.add_range(&actor_a, 10, 20);
assert!(clock_a.contains(&actor_a, 10));
assert!(clock_a.contains(&actor_a, 11));
assert!(!clock_a.contains(&actor_a, 21));Sourcepub fn contains(&self, actor: &A, seq: u64) -> bool
pub fn contains(&self, actor: &A, seq: u64) -> bool
Checks if an event is part of the clock.
§Examples
use threshold::*;
let actor_a = "A";
let mut clock = VClock::new();
assert!(!clock.contains(&actor_a, 1));
clock.add(&actor_a, 1);
assert!(clock.contains(&actor_a, 1));
assert!(!clock.contains(&actor_a, 2));
clock.add(&actor_a, 3);
assert!(clock.contains(&actor_a, 1));
assert!(clock.contains(&actor_a, 2));
assert!(clock.contains(&actor_a, 3));Sourcepub fn frontier(&self) -> VClock<A>
pub fn frontier(&self) -> VClock<A>
Returns the clock frontier.
§Examples
use std::collections::HashMap;
use std::iter::FromIterator;
use threshold::*;
let a = ("A", AboveExSet::from_events(vec![1, 2, 4]));
let b = ("B", AboveExSet::from_events(vec![1, 2, 3, 5, 6]));
let clock = Clock::from(vec![a, b]);
assert_eq!(
clock.frontier(),
VClock::from(vec![("A", MaxSet::from(2)), ("B", MaxSet::from(3))])
);Sourcepub fn frontier_threshold(&self, threshold: usize) -> Option<u64>
pub fn frontier_threshold(&self, threshold: usize) -> Option<u64>
By looking at this Clock’s frontier, it computes the event that’s been
generated in at least threshold actors.
§Examples
use threshold::{clock, *};
let aset = AboveExSet::from_events(vec![1, 2, 4]);
let bset = AboveExSet::from_events(vec![1, 2, 3, 5]);
let clock = Clock::from(vec![("A", aset), ("B", bset)]);
assert_eq!(clock.frontier_threshold(1), Some(3));
assert_eq!(clock.frontier_threshold(2), Some(2));
assert_eq!(clock.frontier_threshold(3), None);
let aset = AboveExSet::from_events(vec![1, 2, 3, 5]);
let bset = AboveExSet::from_events(vec![1, 2, 3, 5]);
let clock = Clock::from(vec![("A", aset), ("B", bset)]);
assert_eq!(clock.frontier_threshold(1), Some(3));
assert_eq!(clock.frontier_threshold(2), Some(3));
let clock = clock::vclock_from_seqs(vec![2, 1, 3]);
assert_eq!(clock.frontier_threshold(1), Some(3));
assert_eq!(clock.frontier_threshold(2), Some(2));
assert_eq!(clock.frontier_threshold(3), Some(1));
let clock = clock::vclock_from_seqs(vec![4, 4, 5, 3, 2]);
assert_eq!(clock.frontier_threshold(1), Some(5));
assert_eq!(clock.frontier_threshold(2), Some(4));
assert_eq!(clock.frontier_threshold(3), Some(4));
assert_eq!(clock.frontier_threshold(4), Some(3));
assert_eq!(clock.frontier_threshold(5), Some(2));
assert_eq!(clock.frontier_threshold(6), None);Sourcepub fn join(&mut self, other: &Self)
pub fn join(&mut self, other: &Self)
Merges clock other passed as argument into self.
After merge, all events in other are events in self.
§Examples
use threshold::*;
let actor_a = "A";
let mut clock_a = VClock::new();
let mut clock_b = VClock::new();
clock_a.next(&actor_a);
let event = clock_a.next(&actor_a);
clock_b.join(&clock_a);
assert!(clock_b.contains(&actor_a, event));Sourcepub fn meet(&mut self, other: &Self)
pub fn meet(&mut self, other: &Self)
Intersects clock other passed as argument with self.
After intersection, only the common events are in self.
§Examples
use threshold::*;
let actor_a = "A";
let mut clock_a = VClock::new();
let mut clock_b = VClock::new();
let event = clock_a.next(&actor_a);
clock_b.meet(&clock_a);
assert!(!clock_b.contains(&actor_a, event));
clock_b.next(&actor_a);
clock_b.meet(&clock_a);
assert!(clock_b.contains(&actor_a, event));Sourcepub fn iter<'a>(&self) -> Iter<'_, A, E> ⓘ
pub fn iter<'a>(&self) -> Iter<'_, A, E> ⓘ
Returns a Clock iterator.
§Examples
use threshold::*;
let mut clock = VClock::new();
clock.next(&"A");
clock.next(&"A");
clock.next(&"B");
for (&actor, eset) in clock.iter() {
match actor {
"A" => assert_eq!(eset, &MaxSet::from_event(2)),
"B" => assert_eq!(eset, &MaxSet::from_event(1)),
_ => panic!("unexpected actor name"),
}
}Sourcepub fn iter_mut<'a>(&mut self) -> IterMut<'_, A, E> ⓘ
pub fn iter_mut<'a>(&mut self) -> IterMut<'_, A, E> ⓘ
Returns a Clock mutable iterator.
§Examples
use threshold::*;
let mut clock = VClock::new();
clock.next(&"A");
clock.next(&"A");
clock.next(&"B");
for (&actor, eset) in clock.iter_mut() {
if actor == "A" {
eset.add_event(3);
}
}
let max_set = clock.get(&"A").expect("there should be an event set");
assert_eq!(max_set, &MaxSet::from_event(3));pub fn subtracted(&self, other: &Self) -> HashMap<A, Vec<u64>>
Trait Implementations§
Source§impl<'de, A, E> Deserialize<'de> for Clock<A, E>
impl<'de, A, E> Deserialize<'de> for Clock<A, E>
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<A: Actor, E: EventSet> IntoIterator for Clock<A, E>
impl<A: Actor, E: EventSet> IntoIterator for Clock<A, E>
Source§fn into_iter(self) -> Self::IntoIter
fn into_iter(self) -> Self::IntoIter
Returns a Clock into-iterator.
§Examples
use threshold::*;
let mut clock = VClock::new();
clock.next(&"A");
clock.next(&"A");
clock.next(&"B");
for (actor, eset) in clock {
match actor {
"A" => assert_eq!(eset, MaxSet::from_event(2)),
"B" => assert_eq!(eset, MaxSet::from_event(1)),
_ => panic!("unexpected actor name"),
}
}