Tabulate

Trait Tabulate 

Source
pub trait Tabulate: Sized {
    type Counter: Counter;

    // Required method
    fn counter() -> &'static Self::Counter;

    // Provided method
    fn instances() -> <Self::Counter as Counter>::Primitive { ... }
}
Expand description

Track the population of Self.

Required Associated Types§

Source

type Counter: Counter

The type of the counter used to track instances of Self.

Required Methods§

Source

fn counter() -> &'static Self::Counter

Produces a reference to the counter tracking instances of Self.

Provided Methods§

Source

fn instances() -> <Self::Counter as Counter>::Primitive

Produces the number of extant instances of T.

Examples found in repository?
examples/derive-distributedcounter.rs (line 34)
32fn main() {
33    // you can now query the number of extant instances of `Foo`!
34    assert_eq!(Foo::<i8>::instances(), 0);
35    assert_eq!(Foo::<u8>::instances(), 0);
36
37    // the same counter is shared for all generic instantiations
38    let mut bar: Vec<Foo<i8>> = vec![Foo::new(0i8); 10];
39
40    assert_eq!(Foo::<i8>::instances(), 10);
41    assert_eq!(Foo::<u8>::instances(), 10);
42
43    let _baz: Vec<Foo<u8>> = vec![Foo::new(0u8); 5];
44
45    assert_eq!(Foo::<i8>::instances(), 15);
46    assert_eq!(Foo::<u8>::instances(), 15);
47
48    let _ = bar.drain(0..5);
49
50    assert_eq!(Foo::<i8>::instances(), 10);
51    assert_eq!(Foo::<u8>::instances(), 10);
52}
More examples
Hide additional examples
examples/derive-relaxedcounter.rs (line 33)
31fn main() {
32    // you can now query the number of extant instances of `Foo`!
33    assert_eq!(Foo::<i8>::instances(), 0);
34    assert_eq!(Foo::<u8>::instances(), 0);
35
36    // the same counter is shared for all generic instantiations
37    let mut bar: Vec<Foo<i8>> = vec![Foo::new(0i8); 10];
38
39    assert_eq!(Foo::<i8>::instances(), 10);
40    assert_eq!(Foo::<u8>::instances(), 10);
41
42    let _baz: Vec<Foo<u8>> = vec![Foo::new(0u8); 5];
43
44    assert_eq!(Foo::<i8>::instances(), 15);
45    assert_eq!(Foo::<u8>::instances(), 15);
46
47    let _ = bar.drain(0..5);
48
49    assert_eq!(Foo::<i8>::instances(), 10);
50    assert_eq!(Foo::<u8>::instances(), 10);
51}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§