pub struct CounterI32 { /* private fields */ }
Expand description

An atomic counter using AtomicI32 / (core::i32).

Behavior

  1. The default ordering is Sequentially Consistent.
  2. The ordering used for atomic operations is customizable for operations ending in with_ordering.
  3. The choice of ordering intentionally impacts ALMOST EVERYTHING about how this counter works, including de/serialization, incrementing, decrementing, equality comparisons, partial ordering comparisons, etc.
  4. Total (non-partial) ordering comparisons always use the default ordering.
  5. Unlike the underlying AtomicI32, this will not wrap on overflow.

Ordering

  • PartialEq is implemented such that counters with differing orderings are never equal.
  • PartialOrd is implemented such that counters with differing (atomic) orderings produce no (comparison) ordering.
  • (Saturating) arithmetic operations are implemented such that differing atomic orderings between the operands are ignored!

Miscellaneous

You can use the to_x method to convert to any type that implements From<i32>

Implementations§

source§

impl CounterI32

source

pub const MAX: i32 = 2_147_483_647i32

source

pub const MIN: i32 = -2_147_483_648i32

source

pub const DEFAULT_ORDERING: Ordering = Ordering::SeqCst

source

pub const fn new() -> Self

Instantiate

source

pub const fn new_with_ordering(ordering: Ordering) -> Self

Instantiate with ordering

source

pub const fn new_from_offset(offset: i32) -> Self

Instantiate with offset value

source

pub const fn new_from_offset_with_ordering( offset: i32, ordering: Ordering ) -> Self

Instantiate with offset value and ordering

source

pub fn get(&self) -> i32

Get current value with the default ordering

use width_counters::{CounterI32 as C };
use i32 as U;
let c = C::new();
assert_eq!(c.get(), 0, "get returns initial value");
c.inc_one();
c.inc_one();
c.inc_one();
assert_eq!(c.get(), 3, "get returns post-increment value");
source

pub fn get_with_ordering(&self, ordering: Ordering) -> i32

Get current value with a specific ordering

source

pub fn get_i128(&self) -> i128

Convenience method for getting the current value as i128

source

pub fn to_x<X: From<i32>>(&self) -> X

Convert to some type that impls From i32.

source§

impl CounterI32

source

pub fn inc_one(&self)

Increment by one

use width_counters::{CounterI32 as C };
use core::ops::*; 
use i32 as U;
let offset = U::MAX/2;
let c = C::new_from_offset(offset);
let m = 20;
(0..m).for_each(|_| { c.inc_one(); });
assert_eq!(c.get(), (offset).add(20), "counter must Increment/add number of times given as per sequential ordering");
let d = C::new_from_offset(U::MAX);
d.inc_one();
d.inc_one();
assert_eq!(d.get(), U::MAX, "counter must stop at MAX ");
source

pub fn inc_one_with_ordering(&self, ordering: Ordering)

Increment by one with ordering

source

pub fn inc_by(&self, amount: i32)

Increment by specified amount

use width_counters::{CounterI32 as C };
use core::ops::*; 
use i32 as U;
let offset = U::MAX/2;
let c = C::new_from_offset(offset);
let m = 20;
(0..m).for_each(|_| { c.inc_by(2); });
assert_eq!((c.get() as i128).sub((20*2) as i128), ((offset) as i128), "counter must Increment by specified amount");
source

pub fn inc_by_with_ordering(&self, amount: i32, ordering: Ordering)

Increment by specified amount with ordering

use width_counters::{CounterI32 as C };
use i32 as U;
use core::ops::*; 
let m = 3i32;
let offset = U::MAX/2;
let d = C::new_from_offset(U::MAX.sub(m * 2));
d.inc_by(m);
d.inc_by(m);
d.inc_by(m);
assert_eq!(d.get(), U::MAX, "counter must stop at MAX");
source§

impl CounterI32

source

pub fn dec_one(&self)

Decrement by one

use width_counters::{CounterI32 as C };
use core::ops::*; 
use i32 as U;
let offset = U::MAX/2;
let c = C::new_from_offset(offset);
let m = 20;
(0..m).for_each(|_| { c.dec_one(); });
assert_eq!(c.get(), (offset).sub(20), "counter must Decrement/sub number of times given as per sequential ordering");
let d = C::new_from_offset(U::MIN);
d.dec_one();
d.dec_one();
assert_eq!(d.get(), U::MIN, "counter must stop at MIN ");
source

pub fn dec_one_with_ordering(&self, ordering: Ordering)

Decrement by one with ordering

source

pub fn dec_by(&self, amount: i32)

Decrement by specified amount

use width_counters::{CounterI32 as C };
use core::ops::*; 
use i32 as U;
let offset = U::MAX/2;
let c = C::new_from_offset(offset);
let m = 20;
(0..m).for_each(|_| { c.dec_by(2); });
assert_eq!((c.get() as i128).add((20*2) as i128), ((offset) as i128), "counter must Decrement by specified amount");
source

pub fn dec_by_with_ordering(&self, amount: i32, ordering: Ordering)

Decrement by specified amount with ordering

use width_counters::{CounterI32 as C };
use i32 as U;
use core::ops::*; 
let m = 3i32;
let offset = U::MAX/2;
let d = C::new_from_offset(U::MIN.add(m * 2));
d.dec_by(m);
d.dec_by(m);
d.dec_by(m);
assert_eq!(d.get(), U::MIN, "counter must stop at MIN");

Trait Implementations§

source§

impl Add<CounterI32> for CounterI32

source§

fn add(self, rhs: Self) -> Self::Output

  • This operation is implemented with saturating arithmetic
  • This operation IGNORES dissimilar atomic orderings!
§

type Output = CounterI32

The resulting type after applying the + operator.
source§

impl AsRef<Ordering> for CounterI32

source§

fn as_ref(&self) -> &Ordering

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Clone for CounterI32

source§

fn clone(&self) -> Self

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 Debug for CounterI32

source§

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

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

impl Default for CounterI32

source§

fn default() -> Self

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

impl Display for CounterI32

source§

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

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

impl Div<CounterI32> for CounterI32

source§

fn div(self, rhs: Self) -> Self::Output

  • This operation is implemented with saturating arithmetic
  • This operation IGNORES dissimilar atomic orderings!
§

type Output = CounterI32

The resulting type after applying the / operator.
source§

impl From<&CounterI32> for i32

source§

fn from(counter: &CounterI32) -> Self

Converts to this type from the input type.
source§

impl From<i32> for CounterI32

source§

fn from(x: i32) -> Self

Converts to this type from the input type.
source§

impl Hash for CounterI32

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 Mul<CounterI32> for CounterI32

source§

fn mul(self, rhs: Self) -> Self::Output

  • This operation is implemented with saturating arithmetic
  • This operation IGNORES dissimilar atomic orderings!
§

type Output = CounterI32

The resulting type after applying the * operator.
source§

impl Ord for CounterI32

source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

impl PartialEq<CounterI32> for CounterI32

PartialEq is only equal when orderings are equal

use width_counters::{CounterI32 as C };
use core::sync::atomic::Ordering;
let a = C::new_from_offset_with_ordering(33, Ordering::Relaxed);
let b = C::new_from_offset_with_ordering(33, Ordering::Relaxed);
assert_eq!(a, b, "counters must be equal when counts and orderings are equal");
assert_eq!(a, b, "counters must be equal when counts and orderings are equal");
let m = 20;
(0..m).for_each(|_| { a.inc_one(); b.inc_one(); });
assert_eq!(a, b, "counters must be equal after counting same amount");
assert_eq!(a, b, "counters must be equal after counting same amount");
a.inc_one();
assert_ne!(a, b, "counters must not be equal after counting different amounts");
let c = C::new_from_offset_with_ordering(44, Ordering::Relaxed);
let d = C::new_from_offset_with_ordering(44, Ordering::Release);
assert_ne!(c, d, "ordering-inequal counters must not be equal with same count");
source§

fn eq(&self, rhs: &Self) -> 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 PartialOrd<CounterI32> for CounterI32

PartialOrd only produces cmp ordering when atomic orderings are equal

use width_counters::{CounterI32 as C };
use core::sync::atomic::Ordering;
let a = C::new_from_offset_with_ordering(32, Ordering::Relaxed);
let b = C::new_from_offset_with_ordering(33, Ordering::Relaxed);
assert!(a < b, "same-cmp::ordering counters must be ordered by when counts");
assert!(b > a, "same-cmp::ordering counters must be ordered by when counts");
let m = 20;
(0..m).for_each(|_| { a.inc_one(); b.inc_one(); });
assert!(a < b, "cmp::ordering preserved after counting same amount");
assert!(b > a, "cmp::ordering preserved after counting same amount");
source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Sub<CounterI32> for CounterI32

source§

fn sub(self, rhs: Self) -> Self::Output

  • This operation is implemented with saturating arithmetic
  • This operation IGNORES dissimilar atomic orderings!
§

type Output = CounterI32

The resulting type after applying the - operator.
source§

impl Eq for CounterI32

Auto Trait Implementations§

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> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. 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.