6 releases (3 breaking)

0.4.1 Mar 9, 2021
0.3.1 Mar 9, 2021
0.2.1 Oct 1, 2019
0.2.0 Jul 12, 2018
0.1.1 May 14, 2018

#443 in Concurrency

Download history 1823/week @ 2023-06-11 1222/week @ 2023-06-18 2402/week @ 2023-06-25 2202/week @ 2023-07-02 2399/week @ 2023-07-09 3602/week @ 2023-07-16 4193/week @ 2023-07-23 4112/week @ 2023-07-30 3290/week @ 2023-08-06 4486/week @ 2023-08-13 4371/week @ 2023-08-20 3007/week @ 2023-08-27 2718/week @ 2023-09-03 3573/week @ 2023-09-10 2810/week @ 2023-09-17 2550/week @ 2023-09-24

11,665 downloads per month
Used in 4 crates (via wasm-rgame)

MIT license

20KB
410 lines

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


lib.rs:

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

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);

Dependencies

~0.4–0.8MB
~20K SLoC