variant-counter
The efficient and elegant crate to count variants of Rust's Enum.
Get started
#[derive(VariantCount)]
Record your variant
let mut counter = counter;
counter.record;
Erase the record with erase_*()
methods
counter.erase_variant1;
Those erase_*()
methods are under erase
feature flag, and disabled by default.
Check the record with check_*()
methods
assert_eq!;
Those check_*()
methods are under check
feature flag, and disabled by default.
discard()
, or reset()
the data
// Clear the `Enum::Variant1`'s data.
counter.discard;
// Clear all variants data.
counter.reset;
Ignore a variant
If a variant was ignored, it has no effect when you record that variant.
let mut counter = counter;
// Record nothing...
counter.record;
Aggregate your data
let data = counter.aggregate;
Group variants
let counter = counter;
// Group version of aggregate method
let group_data = counter.group_aggregate;
Statistics
// Sum
counter.sum;
// Average
counter.avg;
// Variance
counter.variance;
// Standard deviation
counter.sd;
Weighted
let mut counter = counter;
counter.record;
let w = counter.weighted;
// Sum
w.sum;
// Average
w.avg;
// Variance
w.variance;
// Standard deviation
w.sd;
Macro expand
You can use carg-expand to expand the derived VariantCount
macro.
Here is the expanded code:
/// The concrete counter struct auto-generated by macro.
Feature flags
-
full
: Enable all features. -
check
: Generatecheck
methods for variants. -
erase
: Generateerase
methods for variants. -
stats
: Generate statistics methods, such asavg()
,variance()
, andsd()
, etc. -
std
: Enablestd
crate supported. Enabled by default. Please disable this feature to supportno_std
.