GCounter

Struct GCounter 

Source
pub struct GCounter<V>
where V: Display + AddAssign + Clone + Copy + for<'v> Sum<&'v V> + Ord + Add<Output = V> + Default,
{ pub id: NodeId, pub counter: HashMap<NodeId, V>, pub default_value: V, }

Fields§

§id: NodeId

The identification number of the delta.

§counter: HashMap<NodeId, V>§default_value: V

The default value of V. For example, the default value of i32 is 0.

Implementations§

Source§

impl<V> GCounter<V>
where V: Display + AddAssign + Clone + Copy + for<'v> Sum<&'v V> + Ord + Add<Output = V> + Default,

Source

pub fn new(id: NodeId) -> Self

Source

pub fn local(&self) -> &V

Returns the current value of the local counter.

Source

pub fn read(&self) -> V

Returns the sum of all replica values in the counter.

Source

pub fn inc(&mut self, to_sum: Option<V>)

A mutator that performs an increment of to_sum in the counter.

§Example
use crdt_sample::{GCounter,NodeId};
 
let id = NodeId::new(1, "a".to_string());
let mut gcounter: GCounter<i32> = GCounter::new(id);
 
gcounter.inc(Some(1));
let result_local = gcounter.local();    // Reads the local replica
let result_read = gcounter.read();      // Sum of all counters.
assert_eq!(*result_local, 1);
assert_eq!(result_read, 1);
Source

pub fn join(&mut self, gcounter: &GCounter<V>)

Joins the delta_gcounter with a delta or another delta_gcounter.

§Examples
use crdt_sample::{GCounter, NodeId};
let id1 = NodeId::new(1, "a".to_string());
let id2 = NodeId::new(2, "a".to_string());
let id3 = NodeId::new(3, "a".to_string());
let mut gcounter_1: GCounter<i32> = GCounter::new(id1);
let mut gcounter_2: GCounter<i32> = GCounter::new(id2);
let mut gcounter_3: GCounter<i32> = GCounter::new(id3);

gcounter_1.inc(Some(3));
gcounter_2.inc(Some(5));
gcounter_3.inc(Some(1));

// Join gcounter_1
gcounter_3.join(&gcounter_1);
let result_read = gcounter_3.read();
let result_local = gcounter_3.local();
assert_eq!(result_read, 4);
assert_eq!(*result_local, 1);

// join gcounter_2
gcounter_3.join(&gcounter_2);
let result_read = gcounter_3.read();
let result_local = gcounter_3.local();
assert_eq!(result_read, 9);
assert_eq!(*result_local, 1);

Auto Trait Implementations§

§

impl<V> Freeze for GCounter<V>
where V: Freeze,

§

impl<V> RefUnwindSafe for GCounter<V>
where V: RefUnwindSafe,

§

impl<V> Send for GCounter<V>
where V: Send,

§

impl<V> Sync for GCounter<V>
where V: Sync,

§

impl<V> Unpin for GCounter<V>
where V: Unpin,

§

impl<V> UnwindSafe for GCounter<V>
where V: 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> 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, 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.