{"id":20428911,"url":"https://github.com/ppmathis/const-bitfield","last_synced_at":"2025-04-12T19:24:28.364Z","repository":{"id":62438866,"uuid":"442035882","full_name":"ppmathis/const-bitfield","owner":"ppmathis","description":"Bitfield-like structures with const support for Rust","archived":false,"fork":false,"pushed_at":"2022-11-29T19:42:41.000Z","size":34,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-10T06:23:34.882Z","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":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ppmathis.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-12-27T03:06:24.000Z","updated_at":"2023-03-20T15:33:34.000Z","dependencies_parsed_at":"2023-01-22T05:37:30.737Z","dependency_job_id":null,"html_url":"https://github.com/ppmathis/const-bitfield","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/ppmathis%2Fconst-bitfield","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ppmathis%2Fconst-bitfield/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ppmathis%2Fconst-bitfield/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ppmathis%2Fconst-bitfield/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ppmathis","download_url":"https://codeload.github.com/ppmathis/const-bitfield/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248619706,"owners_count":21134513,"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-11-15T07:29:25.333Z","updated_at":"2025-04-12T19:24:28.329Z","avatar_url":"https://github.com/ppmathis.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# const-bitfield\n\n[![GitHub](https://img.shields.io/static/v1?label=GitHub\u0026message=const-bitfield\u0026style=for-the-badge\u0026logo=github\u0026logoColor=white\u0026color=informational)](https://github.com/ppmathis/const-bitfield)\n[![Crates.io](https://img.shields.io/crates/v/const-bitfield?style=for-the-badge\u0026logo=rust\u0026logoColor=white\u0026color=informational)](https://crates.io/crates/const-bitfield)\n[![docs.rs](https://img.shields.io/docsrs/const-bitfield?style=for-the-badge\u0026label=docs.rs\u0026logo=rust\u0026logoColor=white\u0026color=informational)](https://docs.rs/const-bitfield)\n[![GitHub Actions Status](https://img.shields.io/github/workflow/status/ppmathis/const-bitfield/CI/main?style=for-the-badge\u0026logo=githubactions\u0026logoColor=white)](https://github.com/ppmathis/const-bitfield/actions)\n\nThis crate provides a `bitfield!` macro for generating bitfield-like structures in Rust with support for compile-time\nevaluation using `const`. The following features are currently supported:\n\n- Support of `u8`, `u16`, `u32`, `u64`, `u128` as backing storage types\n- Get and set single-bit values as `bool`\n- Get and set values as unsigned / signed integer types\n- Optional mapping of individual getter to any custom type using `From` trait\n- Optional mapping of individual setter from any custom type using `From` trait\n- Optional support for overlapping fields for union-like behavior\n- Overlapping of fields for union-like implementations\n- Compatibility with `no_std`\n- Usage of arbitrary attributes on struct and fields\n- Usage of arbitrary visibility modifiers on struct and fields\n\nUnfortunately Rust Stable does not currently contain all required features for implementing this crate.\nTo use of this library, you must use a recent Rust Nightly release and add the following feature flags to your crate root:\n\n```rust\n#![feature(const_convert)]      // optional, when using from/into conversion\n#![feature(const_mut_refs)]     // always required\n#![feature(const_trait_impl)]   // always required\n```\n\nHere is a simple example of how this library can be used:\n\n```rust\n#![feature(const_convert)] // optional, when using from/into conversion\n#![feature(const_mut_refs)] // always required\n#![feature(const_trait_impl)] // always required\n\nuse const_bitfield::bitfield;\n\nbitfield! {\n    #[derive(Copy, Clone)]\n    pub struct MyBitField(u32);\n    u8, hello, set_hello: 6, 0;         // hello is stored in bits 0..=6\n    bool, world, set_world: 7;          // world is stored in bit 7\n    // bits 8..=15 are unused\n    u16, goodbye, set_goodbye: 31, 16;   // goodbye is stored in bits 16..=31\n}\n\nfn example() {\n    let mut bf = MyBitField(0);\n\n    bf.set_hello(0b0110110);\n    bf.set_world(true);\n    bf.set_goodbye(0xF00F);\n\n    println!(\"{}\", bf.hello());\n    println!(\"{}\", bf.world());\n    println!(\"{}\", bf.goodbye());\n}\n```\n\nA more detailed example can be found within [tests/bitfield_gdt.rs](tests/bitfield_gdt.rs) which uses the `bitfield!` macro\nto implement parsing and building entries of the [x86 Global Descriptor Table](https://en.wikipedia.org/wiki/Global_Descriptor_Table).\n\nYou may wish to combine this crate with [const-enum](https://crates.io/crates/const-enum) to directly map fields of your bitfield\nfrom and into enums with a `repr` type. To do so, simply use `#[derive(ConstEnum)]` along with e.g. `repr(u8)`. This specific use case\nis also shown as part of the GDT example linked above.\n\n## Additional Credits\nThis crate is heavily inspired by [dzamlo/rust-bitfield](https://github.com/dzamlo/rust-bitfield).\n\nThe API between these two crates is similar, but no compatibility is guaranteed.\nUnlike the other library, this one focuses on `const`-support to allow using it as a helper for complex data structures\nat compile-time without having an impact on runtime performance.\n\n## License\n\nLicensed under either of\n\n* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)\n* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)\n\nat your option.\n\n### Contribution\n\nUnless you explicitly state otherwise, any contribution intentionally submitted\nfor inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any\nadditional terms or conditions.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fppmathis%2Fconst-bitfield","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fppmathis%2Fconst-bitfield","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fppmathis%2Fconst-bitfield/lists"}