{"id":13667860,"url":"https://github.com/Folyd/variant-counter","last_synced_at":"2025-04-26T18:30:41.161Z","repository":{"id":57671477,"uuid":"394929294","full_name":"Folyd/variant-counter","owner":"Folyd","description":"Rust's Enum variant counter","archived":false,"fork":false,"pushed_at":"2021-09-15T08:44:33.000Z","size":140,"stargazers_count":15,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-02T08:05:10.637Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://crates.io/crates/variant_counter","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Folyd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-08-11T09:08:42.000Z","updated_at":"2023-10-02T14:36:36.000Z","dependencies_parsed_at":"2022-08-27T03:50:44.247Z","dependency_job_id":null,"html_url":"https://github.com/Folyd/variant-counter","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Folyd%2Fvariant-counter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Folyd%2Fvariant-counter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Folyd%2Fvariant-counter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Folyd%2Fvariant-counter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Folyd","download_url":"https://codeload.github.com/Folyd/variant-counter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251035122,"owners_count":21526308,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T07:00:52.226Z","updated_at":"2025-04-26T18:30:40.244Z","avatar_url":"https://github.com/Folyd.png","language":"Rust","readme":"# variant-counter\n\n[![Crates.io](https://img.shields.io/crates/v/variant-counter)](https://crates.io/crates/variant_counter)\n[![docs.rs](https://img.shields.io/docsrs/variant_counter)](https://docs.rs/variant_counter)\n\nThe efficient and elegant crate to count variants of Rust's Enum.\n\n## Get started\n\n### `#[derive(VariantCount)]`\n\n```rust\n#[derive(VariantCount)]\npub enum Enum {\n    Variant1,\n    Variant2,\n}\n```\n\n### Record your variant\n\n```rust\nlet mut counter = Enum::counter();\ncounter.record(\u0026Enum::Variant1);\n```\n\n### Erase the record with `erase_*()` methods\n\n```rust\ncounter.erase_variant1();\n```\n\nThose `erase_*()` methods are under `erase` feature flag, and disabled by default.\n\n### Check the record with `check_*()` methods\n\n```rust\nassert_eq!(counter.check_variant1(), 1);\n```\n\nThose `check_*()` methods are under `check` feature flag, and disabled by default.\n\n### `discard()`, or `reset()` the data\n\n```rust\n// Clear the `Enum::Variant1`'s data.\ncounter.discard(\u0026Enum::Variant1);\n\n// Clear all variants data.\ncounter.reset();\n```\n\n### Ignore a variant\n\n```rust\n#[derive(VariantCount)]\npub enum Level {\n    #[counter(ignore)]\n    Trace,\n    Debug,\n    Info,\n    Warn,\n    Error,\n}\n```\n\nIf a variant was ignored, it has no effect when you record that variant.\n\n```rust\nlet mut counter = Level::counter();\n// Record nothing...\ncounter.record(\u0026Level::Trace);\n\n```\n### Aggregate your data\n\n```rust\nlet data = counter.aggregate();\n```\n\n### Group variants\n\n```rust\n#[derive(VariantCount)]\npub enum Platform {\n    #[counter(group = \"Mobile\")]\n    Android,\n    #[counter(group = \"Mobile\")]\n    IOS,\n    #[counter(group = \"Desktop\")]\n    Windows,\n    #[counter(group = \"Desktop\")]\n    Linux,\n    #[counter(group = \"Desktop\")]\n    MacOS,\n    #[counter(group = \"Desktop\")]\n    ChromeOS,\n    Others,\n}\n\nlet counter = Platform::counter();\n// Group version of aggregate method\nlet group_data = counter.group_aggregate();\n```\n### Statistics\n\n```rust\n// Sum\ncounter.sum();\n\n// Average\ncounter.avg();\n\n// Variance\ncounter.variance();\n\n// Standard deviation\ncounter.sd();\n```\n\n### Weighted\n\n```rust\n#[derive(VariantCount)]\nenum Rating {\n    #[counter(weight = 1)]\n    Hated,\n    #[counter(weight = 2)]\n    Disliked,\n    #[counter(weight = 3)]\n    Ok,\n    #[counter(weight = 4)]\n    Liked,\n    #[counter(weight = 5)]\n    Loved,\n}\n\nlet mut counter = Rating::counter();\ncounter.record(\u0026Rating::Loved);\n\nlet w = counter.weighted();\n\n// Sum\nw.sum();\n\n// Average\nw.avg();\n\n// Variance\nw.variance();\n\n// Standard deviation\nw.sd();\n```\n\n## Macro expand\n\nYou can use [carg-expand](https://crates.io/crates/cargo-expand) to expand the derived `VariantCount` macro. \nHere is the expanded code:\n\n```rust\nenum Enum {\n    Variant1,\n    Variant2,\n}\nimpl Enum {\n    #[inline]\n    const fn variant_count() -\u003e usize {\n        2usize\n    }\n}\nimpl variant_counter::VariantCount for Enum {\n    type Counter = EnumCounter;\n    fn counter() -\u003e Self::Counter {\n        EnumCounter::new()\n    }\n}\n/// The concrete counter struct auto-generated by macro.\n#[must_use]\nstruct EnumCounter {\n    /// An array store the frequency of each variant which not be ignored.\n    frequency: [usize; 2usize],\n}\nimpl EnumCounter {\n    const fn new() -\u003e EnumCounter {\n        EnumCounter {\n            frequency: [0; 2usize],\n        }\n    }\n    /// Record a variant. It has no effect if you record an ignored variant.\n    fn record(\u0026mut self, target: \u0026Enum) {\n        let pair = match target {\n            Enum::Variant1 =\u003e Some(0usize),\n            Enum::Variant2 =\u003e Some(1usize),\n            _ =\u003e None,\n        };\n        if let Some(index) = pair {\n            self.frequency[index] = self.frequency[index].saturating_add(1);\n        }\n    }\n    /// Discard the record of the target variant.\n    /// It has no effect if you discard an ignored variant.\n    fn discard(\u0026mut self, target: \u0026Enum) {\n        let index = match target {\n            Enum::Variant1 =\u003e Some(0usize),\n            Enum::Variant2 =\u003e Some(1usize),\n            _ =\u003e None,\n        };\n        if let Some(index) = index {\n            self.frequency[index] = 0;\n        }\n    }\n    /// Reset the records.\n    fn reset(\u0026mut self) {\n        self.frequency = [0; 2usize];\n    }\n    /// Aggregate the data to a HashMap.\n    #[cfg(feature = \"std\")]\n    fn aggregate(\u0026self) -\u003e std::collections::HashMap\u003c\u0026'static str, usize\u003e {\n        IntoIterator::into_iter([\n            (\"Variant1\", self.frequency[0usize]),\n            (\"Variant2\", self.frequency[1usize]),\n        ])\n        .collect()\n    }\n    /// Get the sum of frequency.\n    #[inline]\n    fn sum(\u0026self) -\u003e usize {\n        self.frequency.iter().sum()\n    }\n}\n```\n\n## Feature flags\n\n- `full`: Enable all features.\n\n- `check`: Generate `check` methods for variants.\n\n- `erase`: Generate `erase` methods for variants.\n\n- `stats`: Generate statistics methods, such as `avg()`, `variance()`, and `sd()`, etc.\n\n- `std`: Enable `std` crate supported. Enabled by default. Please disable this feature to support `no_std`.","funding_links":[],"categories":["Rust","库"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FFolyd%2Fvariant-counter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FFolyd%2Fvariant-counter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FFolyd%2Fvariant-counter/lists"}