Countme: quickly count the number of instances of a type

https://crates.io/crates/countme

A library to quickly get the live/total/max counts of allocated instances.

#[derive(Default)]
struct Widget {
  _t: countme::Token<Self>,
}

impl countme::CountMe for Widget {
    fn store() -> &'static countme::Store {
        static S: countme::Store = countme::Store::new();
        &S
    }
}

let w1 = Widget::default();
let w2 = Widget::default();
let w3 = Widget::default();
drop(w1);

let counts = countme::get::<Widget>();
assert_eq!(counts.live, 2);
assert_eq!(counts.max, 3);
assert_eq!(counts.total, 3);
4 Likes

Published a 2.0 with significantly more pleasant API:

#[derive(Default)]
struct Widget {
  _c: countme::Count<Self>,
}

let w1 = Widget::default();
let w2 = Widget::default();
let w3 = Widget::default();
drop(w1);

let counts = countme::get::<Widget>();
assert_eq!(counts.live, 2);
assert_eq!(counts.max_live, 3);
assert_eq!(counts.total, 3);

eprintln!("{}", countme::get_all());
3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.