[][src]Struct cvrdt_exposition::g_counter::GCounter

pub struct GCounter {
    pub id: usize,
    pub counts: Vec<u64>,
}

A vectorized counter that can only grow

Panics

Any function involving two or more GCounters (viz. le and merge) will panic (via assert_eq!) if their counts vectors are not the same length. I'd prefer to check this at compile time (as much as possible) instead, but

  • avoiding C++'s template mess is part of what makes Rust great
  • Rust doesn't have const generics yet
  • this library is meant to be as simple and expository as possible, so I'd like to avoid fancier things like generic_array

As mentioned above, operations panic when trying dealing with two or more GCounters of incompatible sizes:

This example panics
// this will panic
use cvrdt_exposition::{GCounter, Grow};
let x = GCounter::new((0, vec![0]));
let y = GCounter::new((1, vec![0, 0]));
x.merge(&y);

Difference from references

In the comprehensive study paper and the Wikipedia article, the vectorized GCounter presumes a local myID() function that tells our local GCounter the index to update in its counts array. This detail isn't necessary for understanding how their pseudocode works, but it is required if you're trying to implement a GCounter in real code. As such, we explicitly include the id as a member of our GCounter struct, and make the arbitrary choice that when merging two GCounters, we take the minimum of their two ids as the new one.

Examples

Example usage, including demonstrating some properties:

use cvrdt_exposition::{GCounter, Grow};
let mut x = GCounter::new((0, vec![0; 3]));
x.add(());
assert_eq!(x.payload(), (0, vec![1, 0, 0]));
assert_eq!(x.query(&()), 1);
let mut y = GCounter::new((1, vec![0; 3]));
y.add(());
y.add(());
assert_eq!(x.merge(&y).payload(), (0, vec![1, 2, 0]));
let z = GCounter::new((2, vec![0, 0, 3]));
assert!(x.le(&x.merge(&y).merge(&z)));
assert_eq!(x.merge(&y).merge(&z).payload(), (0, vec![1, 2, 3]));
assert_eq!(x.merge(&y.merge(&z)).payload(), x.merge(&y).merge(&z).payload());

Fields

id: usize

The index for this local GCounter where all increments occur

counts: Vec<u64>

The vector of counts

Trait Implementations

impl Clone for GCounter[src]

impl Debug for GCounter[src]

impl Grow for GCounter[src]

type Payload = (usize, Vec<u64>)

The internal state of our CvRDT; sufficient to build a new copy via new. Required to implement Eq for testing and verification. Read more

type Update = ()

Message to update our internal state

type Query = ()

Message to query the Value of our CvRDT

type Value = u64

The response to a Query

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.