{"id":16948732,"url":"https://github.com/dentosal/num_enum_simplified","last_synced_at":"2025-03-21T09:46:58.091Z","repository":{"id":83101263,"uuid":"223678053","full_name":"Dentosal/num_enum_simplified","owner":"Dentosal","description":"Procedural macros to make inter-operation between primitives and enums easier. Simplified from the num_enum crate.","archived":false,"fork":false,"pushed_at":"2019-11-24T01:43:39.000Z","size":43,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-26T06:25:32.497Z","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":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Dentosal.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-11-24T01:42:56.000Z","updated_at":"2019-11-24T01:43:44.000Z","dependencies_parsed_at":null,"dependency_job_id":"2979645f-880e-4101-8d62-dc7dcdbdffdc","html_url":"https://github.com/Dentosal/num_enum_simplified","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dentosal%2Fnum_enum_simplified","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dentosal%2Fnum_enum_simplified/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dentosal%2Fnum_enum_simplified/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dentosal%2Fnum_enum_simplified/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Dentosal","download_url":"https://codeload.github.com/Dentosal/num_enum_simplified/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244776273,"owners_count":20508503,"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-13T21:52:15.916Z","updated_at":"2025-03-21T09:46:58.064Z","avatar_url":"https://github.com/Dentosal.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"num_enum :: simplified\n======================\n\nSimplified version of the `num_enum` crate.\n\nProcedural macros to make inter-operation between primitives and enums easier.\nThis crate is no_std compatible.\n\n\nTurning an enum into a primitive\n--------------------------------\n\n```rust\nuse num_enum::IntoPrimitive;\n\n#[derive(IntoPrimitive)]\n#[repr(u8)]\nenum Number {\n    Zero,\n    One,\n}\n\nfn main() {\n    let zero: u8 = Number::Zero.into();\n    assert_eq!(zero, 0u8);\n}\n```\n\n`num_enum`'s `IntoPrimitive` is more type-safe than using `as`, because `as` will silently truncate - `num_enum` only derives `From` for exactly the discriminant type of the enum.\n\nAttempting to turn a primitive into an enum with try_from\n----------------------------------------------\n\n```rust\nuse num_enum::TryFromPrimitive;\nuse core::convert::TryFrom;\n\n#[derive(Debug, Eq, PartialEq, TryFromPrimitive)]\n#[repr(u8)]\nenum Number {\n    Zero,\n    One,\n}\n\nfn main() {\n    let zero = Number::try_from(0u8);\n    assert_eq!(zero, Ok(Number::Zero));\n\n    let three = Number::try_from(3u8);\n    assert_eq!(three, Err(()));\n}\n```\n\nUnsafely turning a primitive into an enum with from_unchecked\n-------------------------------------------------------------\n\nIf you're really certain a conversion will succeed, and want to avoid a small amount of overhead, you can use unsafe\ncode to do this conversion. Unless you have data showing that the match statement generated in the `try_from` above is a\nbottleneck for you, you should avoid doing this, as the unsafe code has potential to cause serious memory issues in\nyour program.\n\n```rust\nuse num_enum::UnsafeFromPrimitive;\n\n#[derive(Debug, Eq, PartialEq, UnsafeFromPrimitive)]\n#[repr(u8)]\nenum Number {\n    Zero,\n    One,\n}\n\nfn main() {\n    assert_eq!(\n        Number::Zero,\n        unsafe { Number::from_unchecked(0_u8) },\n    );\n    assert_eq!(\n        Number::One,\n        unsafe { Number::from_unchecked(1_u8) },\n    );\n}\n\nunsafe fn undefined_behavior() {\n    let _ = Number::from_unchecked(2); // 2 is not a valid discriminant!\n}\n```\n\nOptional features\n-----------------\n\nSome enum values may be composed of complex expressions, for example:\n\n```rust\nenum Number {\n    Zero = (0, 1).0,\n    One = (0, 1).1,\n}\n```\n\nTo cut down on compile time, these are not supported by default, but if you enable the `complex-expressions` feature of your dependency on `num_enum`, these should start working.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdentosal%2Fnum_enum_simplified","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdentosal%2Fnum_enum_simplified","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdentosal%2Fnum_enum_simplified/lists"}