pub struct GCounter<V>{
pub id: NodeId,
pub counter: HashMap<NodeId, V>,
pub default_value: V,
}Fields§
§id: NodeIdThe identification number of the delta.
counter: HashMap<NodeId, V>§default_value: VThe default value of V. For example, the default value of i32 is 0.
Implementations§
Source§impl<V> GCounter<V>
impl<V> GCounter<V>
pub fn new(id: NodeId) -> Self
Sourcepub fn inc(&mut self, to_sum: Option<V>)
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);Sourcepub fn join(&mut self, gcounter: &GCounter<V>)
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more