{"id":17968771,"url":"https://github.com/robbepop/enum-tag","last_synced_at":"2025-03-25T10:32:39.276Z","repository":{"id":146338691,"uuid":"618154965","full_name":"Robbepop/enum-tag","owner":"Robbepop","description":"Proc. macro to generate C-like `enum` tags.","archived":false,"fork":false,"pushed_at":"2023-03-28T14:49:17.000Z","size":36,"stargazers_count":13,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-20T01:11:18.120Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/Robbepop.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"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":"2023-03-23T21:36:40.000Z","updated_at":"2024-08-05T07:55:33.000Z","dependencies_parsed_at":"2023-07-11T07:15:53.872Z","dependency_job_id":null,"html_url":"https://github.com/Robbepop/enum-tag","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Robbepop%2Fenum-tag","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Robbepop%2Fenum-tag/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Robbepop%2Fenum-tag/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Robbepop%2Fenum-tag/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Robbepop","download_url":"https://codeload.github.com/Robbepop/enum-tag/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245444468,"owners_count":20616384,"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-10-29T14:41:31.789Z","updated_at":"2025-03-25T10:32:39.264Z","avatar_url":"https://github.com/Robbepop.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"| Continuous Integration |  Documentation   |      Crates.io       |\n|:----------------------:|:----------------:|:--------------------:|\n| [![ci][1]][2]          | [![docs][3]][4] | [![crates][5]][6]  |\n\n[1]: https://github.com/Robbepop/enum-tag/actions/workflows/rust.yml/badge.svg\n[2]: https://github.com/Robbepop/enum-tag/actions/workflows/rust.yml\n[3]: https://docs.rs/enum-tag/badge.svg\n[4]: https://docs.rs/enum-tag\n[5]: https://img.shields.io/crates/v/enum-tag.svg\n[6]: https://crates.io/crates/enum-tag\n\n# `#[derive(EnumTag)]`\n\nThis crate provides a proc. macro to derive the `EnumTag` trait for the given Rust `enum`.\nThe `#derive(EnumTag)` proc. macro only works on Rust `enum` types and generates both\n\n- a C-like `enum` type with the same variants as the input Rust `enum`\n  but without all the associated data.\n- a derived implementation of the `EnumTag` trait for the Rust `enum`\n\nThe derived `EnumTag` trait makes it possible to create instances of the generated\nC-like `enum` type as well as link to its definition via `\u003cRustEnum as EnumTag\u003e::Tag`.\n\n## When is this useful?\n\nThis is mostly useful for crates that profit from having a distinct `enum` tag type\nwhile at the same time hosting Rust `enum` types with lots of variants which would\nmake it burdensome to maintain the mirroring between both `enum` type and `enum` tag type.\n\nThe motivation for this crate was a Wasm interpreter that represents its instructions\nas an enum but also wants to access the opcodes of the instructions without their data.\nIn this example the opcodes are the instruction `enum` tag.\n\n## Example\n\n```rust\nuse ::enum_tag::EnumTag;\n\n#[derive(EnumTag)]\n#[repr(u8)] // Rust needs this for `B = 42`\nenum Foo {\n    A,\n    B = 42,\n    C(i32),\n    D(i32, i64),\n    E { a: i32 },\n    F { a: i32, b: i64 },\n}\n\n/// This is how we can access the generated C-like enum type and name it.\ntype FooTag = \u003cFoo as EnumTag\u003e::Tag;\n\nassert_eq!(FooTag::A, Foo::A.tag());\nassert_eq!(FooTag::B, Foo::B.tag());\nassert_eq!(FooTag::C, Foo::C(1).tag());\nassert_eq!(FooTag::D, Foo::D(2, 3).tag());\nassert_eq!(FooTag::E, Foo::E { a: 4 }.tag());\nassert_eq!(FooTag::F, Foo::F { a: 5, b: 6 }.tag());\n\nassert_eq!(FooTag::B as u8, 42);\n```\n\nThe above `#[derive(EnumTag)]` generates the following Rust code:\n\n```rust\nconst _: () = {\n    #[derive(\n        ::core::fmt::Debug,\n        ::core::clone::Clone,\n        ::core::marker::Copy,\n        ::core::cmp::PartialEq,\n        ::core::cmp::Eq,\n        ::core::cmp::PartialOrd,\n        ::core::cmp::Ord,\n        ::core::hash::Hash,\n    )]\n    pub enum FooTag {\n        A,\n        B = 42,\n        C,\n        D,\n        E,\n        F,\n    }\n\n    impl ::enum_tag::EnumTag for Foo {\n        type Tag = FooTag;\n\n        fn tag(\u0026self) -\u003e \u003cSelf as ::enum_tag::EnumTag\u003e::Tag {\n            match self {\n                Self::A { .. } =\u003e \u003cSelf as ::enum_tag::EnumTag\u003e::Tag::A,\n                Self::B { .. } =\u003e \u003cSelf as ::enum_tag::EnumTag\u003e::Tag::B,\n                Self::C { .. } =\u003e \u003cSelf as ::enum_tag::EnumTag\u003e::Tag::C,\n                Self::D { .. } =\u003e \u003cSelf as ::enum_tag::EnumTag\u003e::Tag::D,\n                Self::E { .. } =\u003e \u003cSelf as ::enum_tag::EnumTag\u003e::Tag::E,\n                Self::F { .. } =\u003e \u003cSelf as ::enum_tag::EnumTag\u003e::Tag::F,\n            }\n        }\n    }\n};\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobbepop%2Fenum-tag","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frobbepop%2Fenum-tag","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobbepop%2Fenum-tag/lists"}