pub struct PnCounter { /* private fields */ }Expand description
An increment/decrement counter: a pair of G-Counters (increments P,
decrements N), join = componentwise.
Model: Crdt.PNCounter ι = (ι → ℕ) × (ι → ℕ) with the Prod lattice.
Deltas bump one side, one coordinate (SafeMesh.deltaBumpP /
deltaBumpN); each side accumulates exactly as a delta G-Counter
(SafeMesh.deltaPNCounter_correct_P / _N), and equal state gives an
equal read (SafeMesh.deltaPNCounter_value_matches).
Implementations§
Source§impl PnCounter
impl PnCounter
Sourcepub fn new(n: usize) -> Self
pub fn new(n: usize) -> Self
Fresh counter for n replicas — the lattice bottom (⊥, ⊥).
§Panics
Panics above GCounter::MAX_REPLICAS or if either side cannot be
allocated. Use Self::try_new for a fallible width check.
Sourcepub fn try_new(n: usize) -> Result<Self, CoordinateError>
pub fn try_new(n: usize) -> Result<Self, CoordinateError>
Reject counts above GCounter::MAX_REPLICAS (4,096) before allocation.
This matches the wire author limit and bounds both sides to 64 KiB.
Allocate both sides through the zeroed allocator path, as on main.
Admitted allocation exhaustion follows the allocator’s out-of-memory policy.
Sourcepub fn apply_inc(&mut self, replica: usize, tally: u64)
pub fn apply_inc(&mut self, replica: usize, tally: u64)
Apply SafeMesh.deltaBumpP replica tally — increment side, one
coordinate on the wire, ⊥ on the other side.
Out-of-range indices are ignored; use Self::try_apply_inc for an error.
Sourcepub fn try_apply_inc(
&mut self,
replica: usize,
tally: u64,
) -> Result<(), CoordinateError>
pub fn try_apply_inc( &mut self, replica: usize, tally: u64, ) -> Result<(), CoordinateError>
Apply an increment-side delta with a checked replica coordinate.
Returns CoordinateError::ReplicaOutOfRange before any mutation if the
index is invalid. Both sides remain unchanged on error. Valid input has
the same effect as Self::apply_inc.
Sourcepub fn apply_dec(&mut self, replica: usize, tally: u64)
pub fn apply_dec(&mut self, replica: usize, tally: u64)
Apply SafeMesh.deltaBumpN replica tally — decrement side.
Out-of-range indices are ignored; use Self::try_apply_dec for an error.
Sourcepub fn try_apply_dec(
&mut self,
replica: usize,
tally: u64,
) -> Result<(), CoordinateError>
pub fn try_apply_dec( &mut self, replica: usize, tally: u64, ) -> Result<(), CoordinateError>
Apply a decrement-side delta with a checked replica coordinate.
Returns CoordinateError::ReplicaOutOfRange before any mutation if the
index is invalid. Both sides remain unchanged on error. Valid input has
the same effect as Self::apply_dec.
Sourcepub fn try_merge(&mut self, other: &PnCounter) -> Result<(), MergeError>
pub fn try_merge(&mut self, other: &PnCounter) -> Result<(), MergeError>
Checked full-state merge: componentwise checked G-Counter merge,
erroring on a replica-count mismatch on either side instead of silently
truncating (WS1). Both sides are length-checked before any mutation, so
the operation is all-or-nothing: on error self is left unchanged.
Sourcepub fn merge(&mut self, other: &PnCounter) -> Result<(), MergeError>
pub fn merge(&mut self, other: &PnCounter) -> Result<(), MergeError>
Full-state merge: componentwise G-Counter merge —
Crdt.pncounter_merge_apply.
Returns MergeError::ReplicaCountMismatch without changing either
side if the replica counts differ, just like PnCounter::try_merge.
Sourcepub fn value(&self) -> i128
pub fn value(&self) -> i128
The counter’s read: (sum of increments) − (sum of decrements) —
Crdt.pncounterValue, an integer with no bound in the model.
The read is exact for every representable state: each side’s total is
below 2^127 (see GCounter::value), so both fit in i128 and their
difference lies strictly inside i128’s range. No conversion here can
change the sign or wrap; a positive total always reads positive.
Trait Implementations§
Source§impl Crdt for PnCounter
impl Crdt for PnCounter
type Delta = PnCounterDelta
Source§fn validate_record(
&self,
id: RecordId,
delta: &Self::Delta,
) -> Result<(), WireError>
fn validate_record( &self, id: RecordId, delta: &Self::Delta, ) -> Result<(), WireError>
Source§fn replica_count(&self) -> Option<usize>
fn replica_count(&self) -> Option<usize>
None for CRDTs without a fixed arity.fn apply_delta(&mut self, delta: Self::Delta)
Source§impl Mergeable for PnCounter
impl Mergeable for PnCounter
Source§fn merge(&mut self, other: &Self) -> Result<(), MergeError>
fn merge(&mut self, other: &Self) -> Result<(), MergeError>
impl Eq for PnCounter
impl StructuralPartialEq for PnCounter
Auto Trait Implementations§
impl Freeze for PnCounter
impl RefUnwindSafe for PnCounter
impl Send for PnCounter
impl Sync for PnCounter
impl Unpin for PnCounter
impl UnsafeUnpin for PnCounter
impl UnwindSafe for PnCounter
Blanket Implementations§
Source§impl<T> Any for Twhere
T: 'static + ?Sized,
impl<T> Any for Twhere
T: 'static + ?Sized,
Source§fn type_id(&self) -> TypeId
fn type_id(&self) -> TypeId
TypeId of self. Read moreSource§impl<T> Borrow<T> for Twhere
T: ?Sized,
impl<T> Borrow<T> for Twhere
T: ?Sized,
Source§fn borrow(&self) -> &T
fn borrow(&self) -> &T
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)self to dest. Read moreSource§impl<T, U> Into<U> for Twhere
U: From<T>,
impl<T, U> Into<U> for Twhere
U: From<T>,
Source§fn into(self) -> U
fn into(self) -> U
Calls U::from(self).
That is, this conversion is whatever the implementation of
From<T> for U chooses to do.