// 1. import these two items: use type_census::{Instance, Tabulate}; // 2. Derive `Tabulate` // This will count instances with a `DistributedCounter` with 32 buckets. #[derive(Clone, Tabulate)] #[Tabulate(Counter = "type_census::counter::DistributedCounter<32>")] pub struct Foo { v: T, // 3. add a field of type `Instance` _instance: Instance, } impl Foo { pub fn new(v: T) -> Self where // 4. add a `Self: Tabulate` bound to constructors Self: Tabulate, { Self { v, // 5. and initialize your `Instance` field like so: _instance: Instance::new(), } } pub fn v(self) -> T { self.v } } fn main() { // you can now query the number of extant instances of `Foo`! assert_eq!(Foo::::instances(), 0); assert_eq!(Foo::::instances(), 0); // the same counter is shared for all generic instantiations let mut bar: Vec> = vec![Foo::new(0i8); 10]; assert_eq!(Foo::::instances(), 10); assert_eq!(Foo::::instances(), 10); let _baz: Vec> = vec![Foo::new(0u8); 5]; assert_eq!(Foo::::instances(), 15); assert_eq!(Foo::::instances(), 15); let _ = bar.drain(0..5); assert_eq!(Foo::::instances(), 10); assert_eq!(Foo::::instances(), 10); }