Clock

Struct Clock 

Source
pub struct Clock<A: Actor, E: EventSet> { /* private fields */ }

Implementations§

Source§

impl<A: Actor, E: EventSet> Clock<A, E>

Source

pub fn new() -> Self

Returns a new Clock instance.

Source

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))])
);
Source

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));
Source

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);
Source

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());
Source

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);
Source

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);
Source

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);
Source

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));
Source

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));
Source

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));
Source

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))])
);
Source

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);
Source

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));
Source

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));
Source

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"),
    }
}
Source

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));
Source

pub fn subtracted(&self, other: &Self) -> HashMap<A, Vec<u64>>

Trait Implementations§

Source§

impl<A: Clone + Actor, E: Clone + EventSet> Clone for Clock<A, E>

Source§

fn clone(&self) -> Clock<A, E>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<A: Actor, E: EventSet> Debug for Clock<A, E>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<A: Default + Actor, E: Default + EventSet> Default for Clock<A, E>

Source§

fn default() -> Clock<A, E>

Returns the “default value” for a type. Read more
Source§

impl<'de, A, E> Deserialize<'de> for Clock<A, E>
where A: Deserialize<'de> + Actor, E: Deserialize<'de> + EventSet,

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<A: Actor, E: EventSet> IntoIterator for Clock<A, E>

Source§

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"),
    }
}
Source§

type Item = (A, E)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<A, E>

Which kind of iterator are we turning this into?
Source§

impl<A: PartialEq + Actor, E: PartialEq + EventSet> PartialEq for Clock<A, E>

Source§

fn eq(&self, other: &Clock<A, E>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<A, E> Serialize for Clock<A, E>
where A: Serialize + Actor, E: Serialize + EventSet,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<A: Eq + Actor, E: Eq + EventSet> Eq for Clock<A, E>

Source§

impl<A: Actor, E: EventSet> StructuralPartialEq for Clock<A, E>

Auto Trait Implementations§

§

impl<A, E> Freeze for Clock<A, E>

§

impl<A, E> RefUnwindSafe for Clock<A, E>

§

impl<A, E> Send for Clock<A, E>
where A: Send, E: Send,

§

impl<A, E> Sync for Clock<A, E>
where A: Sync, E: Sync,

§

impl<A, E> Unpin for Clock<A, E>
where A: Unpin, E: Unpin,

§

impl<A, E> UnwindSafe for Clock<A, E>
where A: UnwindSafe, E: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,