{"id":21093862,"url":"https://github.com/pscheidl/enum-collections","last_synced_at":"2025-05-16T14:32:29.150Z","repository":{"id":65136043,"uuid":"582115732","full_name":"Pscheidl/enum-collections","owner":"Pscheidl","description":"EnumMap for Rust.","archived":false,"fork":false,"pushed_at":"2024-05-16T09:12:57.000Z","size":279,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-05-17T08:33:08.318Z","etag":null,"topics":["collections","data-structures","enum","enum-map","enummap","rust","rust-lang"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Pscheidl.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-12-25T18:35:35.000Z","updated_at":"2024-05-16T09:13:01.000Z","dependencies_parsed_at":"2024-05-10T20:41:27.964Z","dependency_job_id":"c0c98858-be40-4bb6-b2c2-1b776f57c1e0","html_url":"https://github.com/Pscheidl/enum-collections","commit_stats":{"total_commits":71,"total_committers":3,"mean_commits":"23.666666666666668","dds":0.08450704225352113,"last_synced_commit":"fdb77349cd466a03173703b268644eb340c0d121"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pscheidl%2Fenum-collections","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pscheidl%2Fenum-collections/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pscheidl%2Fenum-collections/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pscheidl%2Fenum-collections/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Pscheidl","download_url":"https://codeload.github.com/Pscheidl/enum-collections/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225433484,"owners_count":17473599,"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":["collections","data-structures","enum","enum-map","enummap","rust","rust-lang"],"created_at":"2024-11-19T22:13:10.906Z","updated_at":"2025-05-16T14:32:29.143Z","avatar_url":"https://github.com/Pscheidl.png","language":"Rust","readme":"# EnumMap for Rust\n[![Rust](https://github.com/Pscheidl/enum-map/actions/workflows/rust.yml/badge.svg)](https://github.com/Pscheidl/enum-map/actions/workflows/rust.yml)\n[![Crates.io](https://img.shields.io/crates/v/enum-collections)](https://crates.io/crates/enum-collections)\n[![docs.rs](https://img.shields.io/docsrs/enum-collections)](https://docs.rs/enum-collections/latest/enum_collections/)\n\n[Contribution guide](CONTRIBUTING.md) | [Apache v2 license](LICENSE)\n\nA map of enum variants to values. EnumMap is a fixed-size map, where each variant of the enum is mapped to a value. This implementation of EnumMap uses **safe Rust** only and is a a zero-cost abstraction over an array (**const-sized**), where the index of the array corresponds to the position of the variant in the enum.\n\nBecause it is a thin wrapper over an array, it is stack-allocated by default. Simply `std::boxed::Box`ing it will move it to the heap, at the caller's discretion.\n\n- Indexed by enum variants.\n- IndexMut by enum variants.\n- Debug if the enum is Debug.\n- PartialEq if the value is PartialEq. Same for Eq.\n\nDebug and Eq are optional features. They are enabled by default.\n\n## Usage\n\nPlease refer to the [documentation](https://docs.rs/enum-collections/latest/enum_collections/) for a complete list of features and more in-depth documentation.\n\n```rust\nuse enum_collections::{EnumMap, Enumerated, em, em_default, em_option};\n\n#[derive(Enumerated)]\npub enum Letter {\n   A,\n   B,\n}\n\n// Indexing and mutation\n// The SIZE hint is required until [generic_const_expr](https://github.com/rust-lang/rust/issues/76560) are stabilized.\nlet mut enum_map = EnumMap::\u003cLetter, i32, { Letter::SIZE }\u003e::new_default();\nassert_eq!(0, enum_map[Letter::A]);\nenum_map[Letter::A] = 42;\nassert_eq!(42, enum_map[Letter::A]);\n\n// Construction using macros\n// (Key type, Value type, Key=\u003eValue pairs)\nlet enum_map = em!(Letter, i32,  A=\u003e42, B=\u003e24); // All values set explicitly\nassert_eq!(42, enum_map[Letter::A]);\nassert_eq!(24, enum_map[Letter::B]);\n\n// (Key type, Value type, optional Key=\u003eValue pairs)\nlet enum_map = em_default!(Letter, i32, A =\u003e 42); // Default used for missing values\nassert_eq!(42, enum_map[Letter::A]);\nassert_eq!(i32::default(), enum_map[Letter::B]);\n\nlet enum_map = em_default!(Letter, i32,); // All default\nassert_eq!(i32::default(), enum_map[Letter::A]);\nassert_eq!(i32::default(), enum_map[Letter::B]);\n\n// EnumMap of optional values `Option\u003cV\u003e`, value type is automatically wrapped in `Option`.\n// (Key type, Value type, optional Key=\u003eValue pairs)\nlet enum_map = em_option!(Letter, i32, A =\u003e 42);\nassert_eq!(Some(42), enum_map[Letter::A]);\nassert_eq!(None, enum_map[Letter::B]);\n\n// Constructor with default values\nlet enum_map_default = EnumMap::\u003cLetter, i32, { Letter::SIZE }\u003e::new_default();\nassert_eq!(0, enum_map_default[Letter::A]);\nassert_eq!(0, enum_map_default[Letter::B]);\n\n// Convenience constructor for optional values\nlet mut enum_map_option = EnumMap::\u003cLetter, Option\u003ci32\u003e, { Letter::SIZE }\u003e::new_option();\nassert_eq!(None, enum_map_option[Letter::A]);\nassert_eq!(None, enum_map_option[Letter::B]);\nenum_map_option[Letter::A] = Some(42);\nassert_eq!(Some(42), enum_map_option[Letter::A]);\n\n// Constructor with custom initialization\n#[derive(PartialEq, Eq, Debug)]\nstruct Custom;\nlet enum_map = EnumMap::\u003cLetter, Custom, { Letter::SIZE }\u003e::new(|| Custom);\nassert_eq!(Custom, enum_map[Letter::A]);\nassert_eq!(Custom, enum_map[Letter::B]);\n\n// Custom initialization function with enum variant (key) inspection\nlet enum_map = EnumMap::\u003cLetter, i32, { Letter::SIZE }\u003e::new_inspect(|letter| {\n   match letter {\n      Letter::A =\u003e 42,\n      Letter::B =\u003e 24,\n   }\n});\nassert_eq!(42, enum_map[Letter::A]);\nassert_eq!(24, enum_map[Letter::B]);\n\n// Debug\n#[derive(Enumerated, Debug)]\npub enum LetterDebugDerived {\n   A,\n   B,\n}\nlet enum_map_debug =\n    EnumMap::\u003cLetterDebugDerived, i32, { LetterDebugDerived::SIZE }\u003e::new(|| 42);\nassert_eq!(\"{A: 42, B: 42}\", format!(\"{:?}\", enum_map_debug));\n\n```\n\n\nIterate over enum variants\n\n\n```rust\n#[derive(Enumerated, Debug)]\npub enum Letter {\n   A,\n   B,\n}\n\nLetter::VARIANTS\n    .iter()\n    .for_each(|letter| println!(\"{:?}\", letter));\n```\n\n\n## Features\n\nPortions of functionality are feature-flagged, but enabled by default. This is to allow turning this functionality off when not needed, e.g. `Debug` and `Eq` implementations.\nSee [docs.rs](https://docs.rs/crate/enum-collections/latest/features) for details.\n\n## Benchmarks\n\nInvoke `cargo bench` to run benchmarks. While `EnumMap` operates in pico-seconds, `std::collections::HashMap` in \u003e 10 nanoseconds.\n\n\u003cdetails\u003e\n\u003csummary\u003eBenchmark results\u003c/summary\u003e\n\n```\nEnumMap get             time:   [221.09 ps 221.59 ps 222.21 ps]\nFound 10 outliers among 100 measurements (10.00%)\n  5 (5.00%) high mild\n  5 (5.00%) high severe\n\nEnumMap insert          time:   [230.05 ps 233.38 ps 236.25 ps]\nFound 2 outliers among 100 measurements (2.00%)\n  1 (1.00%) high mild\n  1 (1.00%) high severe\n\nEnumMap new: default    time:   [852.31 ps 853.28 ps 854.37 ps]\nFound 2 outliers among 100 measurements (2.00%)\n  1 (1.00%) low mild\n  1 (1.00%) high mild\n\nEnumMap new: Option::None\n                        time:   [1.7100 ns 1.7110 ns 1.7120 ns]\nFound 2 outliers among 100 measurements (2.00%)\n  1 (1.00%) high mild\n  1 (1.00%) high severe\n\nEnumMap new: provider fn\n                        time:   [791.17 ps 792.38 ps 793.65 ps]\nFound 7 outliers among 100 measurements (7.00%)\n  1 (1.00%) low mild\n  4 (4.00%) high mild\n  2 (2.00%) high severe\n\nEnumMap new: inspecting provider fn\n                        time:   [775.03 ps 776.84 ps 778.92 ps]\nFound 8 outliers among 100 measurements (8.00%)\n  4 (4.00%) high mild\n  4 (4.00%) high severe\n\nstd::collections::HashMap get\n                        time:   [13.433 ns 13.484 ns 13.543 ns]\nFound 8 outliers among 100 measurements (8.00%)\n  3 (3.00%) high mild\n  5 (5.00%) high severe\n\nstd::collections::HashMap insert\n                        time:   [14.094 ns 14.107 ns 14.121 ns]\nFound 4 outliers among 100 measurements (4.00%)\n  1 (1.00%) high mild\n  3 (3.00%) high severe\n\n```\n\n\u003c/details\u003e\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpscheidl%2Fenum-collections","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpscheidl%2Fenum-collections","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpscheidl%2Fenum-collections/lists"}