Struct crdts::pncounter::PNCounter

source ·
pub struct PNCounter<A: Ord> { /* private fields */ }
Expand description

PNCounter allows the counter to be both incremented and decremented by representing the increments (P) and the decrements (N) in separate internal G-Counters.

Merge is implemented by merging the internal P and N counters. The value of the counter is P minus N.

Examples

use crdts::{PNCounter, CmRDT};

let mut a = PNCounter::new();
a.apply(a.inc("A"));
a.apply(a.inc("A"));
a.apply(a.dec("A"));
a.apply(a.inc("A"));

assert_eq!(a.read(), 2.into());

Implementations§

source§

impl<A: Ord + Clone> PNCounter<A>

source

pub fn new() -> Self

Produce a new PNCounter.

source

pub fn inc(&self, actor: A) -> Op<A>

Generate an Op to increment the counter.

source

pub fn dec(&self, actor: A) -> Op<A>

Generate an Op to increment the counter.

source

pub fn inc_many(&self, actor: A, steps: u64) -> Op<A>

Generate an Op to increment the counter by a number of steps.

source

pub fn dec_many(&self, actor: A, steps: u64) -> Op<A>

Generate an Op to decrement the counter by a number of steps.

source

pub fn read(&self) -> BigInt

Return the current value of this counter (P-N).

Trait Implementations§

source§

impl<A: Clone + Ord> Clone for PNCounter<A>

source§

fn clone(&self) -> PNCounter<A>

Returns a copy 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<A: Ord + Clone + Debug> CmRDT for PNCounter<A>

§

type Op = Op<A>

Op defines a mutation to the CRDT. As long as Op’s from one actor are replayed in exactly the same order they were generated by that actor, the CRDT will converge. In other words, we must have a total ordering on each actors operations, while requiring only a partial order over all ops. E.g. Read more
§

type Validation = <GCounter<A> as CmRDT>::Validation

The validation error returned by validate_op.
source§

fn validate_op(&self, op: &Self::Op) -> Result<(), Self::Validation>

Some CRDT’s have stricter requirements on how they must be used. To avoid violating these requirements, CRDT’s provide an interface to optionally validate op’s before they are applied. Read more
source§

fn apply(&mut self, op: Self::Op)

Apply an Op to the CRDT
source§

impl<A: Ord + Clone + Debug> CvRDT for PNCounter<A>

§

type Validation = <GCounter<A> as CvRDT>::Validation

The validation error returned by validate_merge.
source§

fn validate_merge(&self, other: &Self) -> Result<(), Self::Validation>

Some CRDT’s have stricter requirements on how they must be used. To avoid violating these requirements, CRDT’s provide an interface to optionally validate merge compatibility before attempting to merge. Read more
source§

fn merge(&mut self, other: Self)

Merge the given CRDT into the current CRDT.
source§

impl<A: Debug + Ord> Debug for PNCounter<A>

source§

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

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

impl<A: Ord> Default for PNCounter<A>

source§

fn default() -> Self

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

impl<'de, A> Deserialize<'de> for PNCounter<A>where A: Deserialize<'de> + Ord,

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<A: Hash + Ord> Hash for PNCounter<A>

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<A: PartialEq + Ord> PartialEq<PNCounter<A>> for PNCounter<A>

source§

fn eq(&self, other: &PNCounter<A>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<A: Ord> ResetRemove<A> for PNCounter<A>

source§

fn reset_remove(&mut self, clock: &VClock<A>)

Remove data that is strictly smaller than this clock
source§

impl<A> Serialize for PNCounter<A>where A: Serialize + Ord,

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<A: Eq + Ord> Eq for PNCounter<A>

source§

impl<A: Ord> StructuralEq for PNCounter<A>

source§

impl<A: Ord> StructuralPartialEq for PNCounter<A>

Auto Trait Implementations§

§

impl<A> RefUnwindSafe for PNCounter<A>where A: RefUnwindSafe,

§

impl<A> Send for PNCounter<A>where A: Send,

§

impl<A> Sync for PNCounter<A>where A: Sync,

§

impl<A> Unpin for PNCounter<A>

§

impl<A> UnwindSafe for PNCounter<A>where A: RefUnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.
source§

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,

source§

impl<A, T> Val<A> for Twhere A: Ord, T: Clone + Default + ResetRemove<A> + CmRDT,