Struct width_counters::CounterU64
source · pub struct CounterU64 { /* private fields */ }Expand description
A monotonically increasing atomic counter using AtomicU64 / (core::u64).
Behavior
- The default ordering is Sequentially Consistent.
- The ordering used for atomic operations is customizable for operations ending in
with_ordering. - The choice of ordering intentionally impacts EVERYTHING about how this counter works, including de/serialization, incrementing, jumping, equality comparisons, partial ordering comparisons, etc.
- Total (non-partial) ordering comparisons always use the default ordering.
Other Notes
- 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.
- Unlike the underlying AtomicU64, this will not wrap on overflow.
Implementations§
source§impl CounterU64
impl CounterU64
sourcepub const MAX: u64 = 18_446_744_073_709_551_615u64
pub const MAX: u64 = 18_446_744_073_709_551_615u64
Largest representable value
sourcepub const DEFAULT_ORDERING: Ordering = Ordering::SeqCst
pub const DEFAULT_ORDERING: Ordering = Ordering::SeqCst
Default Atomic ordering
sourcepub const fn new_with_ordering(ordering: Ordering) -> Self
pub const fn new_with_ordering(ordering: Ordering) -> Self
Instantiate with ordering
sourcepub const fn new_from_offset(offset: u64) -> Self
pub const fn new_from_offset(offset: u64) -> Self
Instantiate with offset value
sourcepub const fn new_from_offset_with_ordering(
offset: u64,
ordering: Ordering
) -> Self
pub const fn new_from_offset_with_ordering( offset: u64, ordering: Ordering ) -> Self
Instantiate with offset value and ordering
sourcepub fn get(&self) -> u64
pub fn get(&self) -> u64
Get current value with the default ordering
use width_counters::{CounterU64 as C };
use u64 as U;
let c = C::new();
assert_eq!(c.get(), 0, "get returns initial value");
c.inc();
c.inc();
c.inc();
assert_eq!(c.get(), 3, "get returns post-increment value");sourcepub fn get_with_ordering(&self, ordering: Ordering) -> u64
pub fn get_with_ordering(&self, ordering: Ordering) -> u64
Get current value with a specific ordering
sourcepub fn inc(&self)
pub fn inc(&self)
Increment by one
use width_counters::{CounterU64 as C };
use u64 as U;
let c = C::new();
let m = 20;
(0..m).for_each(|_| { c.inc(); });
assert_eq!(c.get(), 20, "counter must count number of times given as per sequential ordering");
let d = C::new_from_offset(U::MAX);
d.inc();
d.inc();
assert_eq!(d.get(), U::MAX, "counter must stop at maximum");sourcepub fn inc_with_ordering(&self, ordering: Ordering)
pub fn inc_with_ordering(&self, ordering: Ordering)
Increment by one with ordering
sourcepub fn jump(&self, amount: u64)
pub fn jump(&self, amount: u64)
Jump by specified amount
use width_counters::{CounterU64 as C };
use u64 as U;
let c = C::default();
let m = 20;
(0..m).for_each(|_| { c.jump(2); });
assert_eq!(c.get(), 20*2, "counter must jump by specified amount");sourcepub fn jump_with_ordering(&self, amount: u64, ordering: Ordering)
pub fn jump_with_ordering(&self, amount: u64, ordering: Ordering)
Jump by specified amount with ordering
use width_counters::{CounterU64 as C };
use u64 as U;
let c = C::new();
let m: U = 20;
(0..m).for_each(|_| { c.jump(2); });
assert_eq!(c.get(), 40, "counter must jump by specified amount");
let d = C::new_from_offset(U::MAX - m);
d.jump(m/3);
d.jump(m/3);
d.jump(m/2);
assert_eq!(d.get(), U::MAX, "counter must stop at maximum");Trait Implementations§
source§impl AsRef<Ordering> for CounterU64
impl AsRef<Ordering> for CounterU64
source§impl Clone for CounterU64
impl Clone for CounterU64
source§impl Debug for CounterU64
impl Debug for CounterU64
source§impl Default for CounterU64
impl Default for CounterU64
source§impl Display for CounterU64
impl Display for CounterU64
source§impl From<&CounterU64> for u64
impl From<&CounterU64> for u64
source§fn from(counter: &CounterU64) -> Self
fn from(counter: &CounterU64) -> Self
Converts to this type from the input type.
source§impl From<u64> for CounterU64
impl From<u64> for CounterU64
source§impl Hash for CounterU64
impl Hash for CounterU64
source§impl Ord for CounterU64
impl Ord for CounterU64
source§impl PartialEq<CounterU64> for CounterU64
impl PartialEq<CounterU64> for CounterU64
PartialEq is only equal when orderings are equal
use width_counters::{CounterU64 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(); b.inc(); });
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();
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§impl PartialOrd<CounterU64> for CounterU64
impl PartialOrd<CounterU64> for CounterU64
PartialOrd only produces cmp ordering when atomic orderings are equal
use width_counters::{CounterU64 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(); b.inc(); });
assert!(a < b, "cmp::ordering preserved after counting same amount");
assert!(b > a, "cmp::ordering preserved after counting same amount");1.0.0 · source§fn le(&self, other: &Rhs) -> bool
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 moreimpl Eq for CounterU64
Auto Trait Implementations§
impl RefUnwindSafe for CounterU64
impl Send for CounterU64
impl Sync for CounterU64
impl Unpin for CounterU64
impl UnwindSafe for CounterU64
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