Expand description
Rust type for a RAII Counter (counts number of held instances,
decrements count on Drop), implemented with Arc<AtomicUsize>.
Useful for tracking the number of holders exist for a handle, tracking the number of transactions that are in-flight, etc.
§Additional Features
Counters can have a size, eg. aCounterwithsize4 adds 4 to the count, and removes 4 when dropped.NotifyHandles can be used for efficient conditional checking, eg. if you want to wait until there are no in-flight transactions, see:CounterBuilder::create_notify/WeakCounterBuilder::create_notifyandNotifyHandle::wait_until_condition.
§Demo
extern crate raii_counter;
use raii_counter::Counter;
let counter = Counter::builder().build();
assert_eq!(counter.count(), 1);
let weak = counter.downgrade();
assert_eq!(weak.count(), 0);
{
let _counter1 = weak.spawn_upgrade();
assert_eq!(weak.count(), 1);
let _counter2 = weak.spawn_upgrade();
assert_eq!(weak.count(), 2);
}
assert_eq!(weak.count(), 0);Structs§
- Counter
- Essentially an AtomicUsize that is clonable and whose count is based on the number of copies (and their size). The count is automatically updated on Drop.
- Counter
Builder - A builder for the
Counter. - Notify
Handle - Struct that enables functionality like waiting to be notified
when the count of a
crate::Counterorcrate::WeakCounterchanges. - Weak
Counter - A ‘weak’
Counterthat does not affect the count. - Weak
Counter Builder - A builder for the
WeakCounter.