pub struct GCounter<const MAX_NODES: usize = 32> { /* private fields */ }Expand description
A Grow-only Counter (G-Counter).
Each node can only increment its own count. The counter value is the sum of all node counts. Merge takes the maximum of each node’s count.
Memory usage: approximately 4 + (MAX_NODES * 8) bytes.
Default capacity of 32 nodes ≈ 260 bytes.
§Example
use peat_lite::{GCounter, NodeId};
let node1 = NodeId::new(1);
let node2 = NodeId::new(2);
let mut counter1 = GCounter::<8>::new();
counter1.increment(node1, 5);
let mut counter2 = GCounter::<8>::new();
counter2.increment(node2, 3);
counter1.merge(&counter2);
assert_eq!(counter1.value(), 8);Implementations§
Source§impl<const MAX_NODES: usize> GCounter<MAX_NODES>
impl<const MAX_NODES: usize> GCounter<MAX_NODES>
Sourcepub fn node_count(&self, node: NodeId) -> u32
pub fn node_count(&self, node: NodeId) -> u32
Get a specific node’s count.
Sourcepub fn increment(&mut self, node: NodeId, delta: u32) -> Option<u32>
pub fn increment(&mut self, node: NodeId, delta: u32) -> Option<u32>
Increment the counter for a specific node.
Returns the new count for that node, or None if the counter
is full and can’t add a new node.
Sourcepub fn merge(&mut self, other: &Self)
pub fn merge(&mut self, other: &Self)
Merge with another counter.
Takes the maximum of each node’s count.
Sourcepub fn node_count_total(&self) -> usize
pub fn node_count_total(&self) -> usize
Get the number of nodes that have contributed to this counter.
Sourcepub fn iter(&self) -> impl Iterator<Item = (NodeId, u32)> + '_
pub fn iter(&self) -> impl Iterator<Item = (NodeId, u32)> + '_
Iterate over (node_id, count) pairs.
Trait Implementations§
impl<const MAX_NODES: usize> Eq for GCounter<MAX_NODES>
Auto Trait Implementations§
impl<const MAX_NODES: usize> Freeze for GCounter<MAX_NODES>
impl<const MAX_NODES: usize> RefUnwindSafe for GCounter<MAX_NODES>
impl<const MAX_NODES: usize> Send for GCounter<MAX_NODES>
impl<const MAX_NODES: usize> Sync for GCounter<MAX_NODES>
impl<const MAX_NODES: usize> Unpin for GCounter<MAX_NODES>
impl<const MAX_NODES: usize> UnsafeUnpin for GCounter<MAX_NODES>
impl<const MAX_NODES: usize> UnwindSafe for GCounter<MAX_NODES>
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