Skip to main content

GCounter

Struct GCounter 

Source
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>

Source

pub const fn new() -> Self

Create a new empty counter.

Source

pub fn value(&self) -> u64

Get the total counter value (sum of all node counts).

Source

pub fn node_count(&self, node: NodeId) -> u32

Get a specific node’s count.

Source

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.

Source

pub fn inc(&mut self, node: NodeId) -> Option<u32>

Increment by 1.

Source

pub fn merge(&mut self, other: &Self)

Merge with another counter.

Takes the maximum of each node’s count.

Source

pub fn node_count_total(&self) -> usize

Get the number of nodes that have contributed to this counter.

Source

pub fn is_empty(&self) -> bool

Check if this counter is empty (all counts are 0 or no nodes).

Source

pub fn clear(&mut self)

Clear all counts.

Source

pub fn iter(&self) -> impl Iterator<Item = (NodeId, u32)> + '_

Iterate over (node_id, count) pairs.

Source

pub fn encode(&self) -> Vec<u8, 258>

Encode to bytes for transmission.

Format: [num_entries: u16][entry1][entry2]... Each entry: [node_id: 4B][count: 4B] = 8 bytes

Source

pub fn decode(data: &[u8]) -> Option<Self>

Decode from bytes.

Trait Implementations§

Source§

impl<const MAX_NODES: usize> Clone for GCounter<MAX_NODES>

Source§

fn clone(&self) -> GCounter<MAX_NODES>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const MAX_NODES: usize> Debug for GCounter<MAX_NODES>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const MAX_NODES: usize> Default for GCounter<MAX_NODES>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<const MAX_NODES: usize> PartialEq for GCounter<MAX_NODES>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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> 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.