raii-counter

Crates.ioraii-counter
lib.rsraii-counter
version0.4.1
sourcesrc
created_at2018-05-13 13:48:32.579576
updated_at2021-03-09 19:24:28.858063
descriptionRAII Counter.
homepagehttps://github.com/DarrenTsung/raii-counter
repositoryhttps://github.com/DarrenTsung/raii-counter
max_upload_size
id65195
size21,331
Darren Tsung (DarrenTsung)

documentation

README

raii-counter

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. a Counter with size 4 adds 4 to the count, and removes 4 when dropped.

Demo

extern crate raii_counter;
use raii_counter::Counter;

let counter = Counter::new();
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);

License: MIT

Commit count: 16

cargo fmt