Expand description

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

Example


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

countme::enable(true);

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

Configuration

By default, the implementation compiles to no-ops. Therefore it is possible to include Count fields into library types.

The enable cargo feature ungates the counting code. The feature can be enabled anywhere in the crate graph.

At run-time, the counters are controlled with enable function. Counting is enabled by default if print_at_exit feature is enabled. Otherwise counting is disabled by default. Call enable(true) early in main to enable:

fn main() {
    countme::enable(std::env::var("COUNTME").is_ok());
}

The code is optimized for the case where counting is not enabled at runtime (counting is a relaxed load and a branch to a function call).

The print_at_exit Cargo feature uses atexit call to print final counts before the program exits (it also enables counting at runtime). Use it only when you can’t modify the main to print counts – atexit is not guaranteed to work with rust’s runtime.

Structs

A collection of counts for all types.

Store this inside your struct as _c: countme::Count<Self>.

Functions

Enable or disable counting at runtime.

Returns the counts for the T type.

Returns a collection of counts for all types.