[−][src]Struct cvrdt_exposition::g_counter::GCounter
A vectorized counter that can only grow
Panics
Any function involving two or more GCounter
s (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 GCounter
s of
incompatible sizes:
// 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 GCounter
s, we take the minimum of their two id
s 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
fn new(payload: Self::Payload) -> Self
[src]
fn payload(&self) -> Self::Payload
[src]
fn add(&mut self, _update: Self::Update)
[src]
fn le(&self, other: &Self) -> bool
[src]
fn merge(&self, other: &Self) -> Self
[src]
fn query(&self, _query: &Self::Query) -> Self::Value
[src]
Auto Trait Implementations
impl RefUnwindSafe for GCounter
impl Send for GCounter
impl Sync for GCounter
impl Unpin for GCounter
impl UnwindSafe for GCounter
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,