pub struct Counter(/* private fields */);Expand description
A thread-safe counter using atomic operations.
This is a wrapper around AtomicUsize that provides a more ergonomic API
for counting operations in concurrent environments.
§Example
use spider_core::state::Counter;
let counter = Counter::new();
counter.inc();
counter.add(5);
assert_eq!(counter.get(), 6);Implementations§
Source§impl Counter
impl Counter
Sourcepub fn with_value(value: usize) -> Counter
pub fn with_value(value: usize) -> Counter
Creates a new counter with the specified initial value.
Sourcepub fn swap(&self, value: usize) -> usize
pub fn swap(&self, value: usize) -> usize
Atomically swaps the counter value and returns the old value.
Sourcepub fn compare_and_swap(&self, current: usize, new: usize) -> usize
pub fn compare_and_swap(&self, current: usize, new: usize) -> usize
Atomically compares and swaps the value.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Counter
impl RefUnwindSafe for Counter
impl Send for Counter
impl Sync for Counter
impl Unpin for Counter
impl UnsafeUnpin for Counter
impl UnwindSafe for Counter
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