Giter VIP home page Giter VIP logo

const-bitfield's Introduction

const-bitfield

GitHub Crates.io docs.rs GitHub Actions Status

This crate provides a bitfield! macro for generating bitfield-like structures in Rust with support for compile-time evaluation using const. The following features are currently supported:

  • Support of u8, u16, u32, u64, u128 as backing storage types
  • Get and set single-bit values as bool
  • Get and set values as unsigned / signed integer types
  • Optional mapping of individual getter to any custom type using From trait
  • Optional mapping of individual setter from any custom type using From trait
  • Optional support for overlapping fields for union-like behavior
  • Overlapping of fields for union-like implementations
  • Compatibility with no_std
  • Usage of arbitrary attributes on struct and fields
  • Usage of arbitrary visibility modifiers on struct and fields

Unfortunately Rust Stable does not currently contain all required features for implementing this crate. To use of this library, you must use a recent Rust Nightly release and add the following feature flags to your crate root:

#![feature(const_convert)]      // optional, when using from/into conversion
#![feature(const_mut_refs)]     // always required
#![feature(const_trait_impl)]   // always required

Here is a simple example of how this library can be used:

#![feature(const_convert)] // optional, when using from/into conversion
#![feature(const_mut_refs)] // always required
#![feature(const_trait_impl)] // always required

use const_bitfield::bitfield;

bitfield! {
    #[derive(Copy, Clone)]
    pub struct MyBitField(u32);
    u8, hello, set_hello: 6, 0;         // hello is stored in bits 0..=6
    bool, world, set_world: 7;          // world is stored in bit 7
    // bits 8..=15 are unused
    u16, goodbye, set_goodbye: 31, 16;   // goodbye is stored in bits 16..=31
}

fn example() {
    let mut bf = MyBitField(0);

    bf.set_hello(0b0110110);
    bf.set_world(true);
    bf.set_goodbye(0xF00F);

    println!("{}", bf.hello());
    println!("{}", bf.world());
    println!("{}", bf.goodbye());
}

A more detailed example can be found within tests/bitfield_gdt.rs which uses the bitfield! macro to implement parsing and building entries of the x86 Global Descriptor Table.

You may wish to combine this crate with const-enum to directly map fields of your bitfield from and into enums with a repr type. To do so, simply use #[derive(ConstEnum)] along with e.g. repr(u8). This specific use case is also shown as part of the GDT example linked above.

Additional Credits

This crate is heavily inspired by dzamlo/rust-bitfield.

The API between these two crates is similar, but no compatibility is guaranteed. Unlike the other library, this one focuses on const-support to allow using it as a helper for complex data structures at compile-time without having an impact on runtime performance.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

const-bitfield's People

Contributors

ppmathis avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

const-bitfield's Issues

Crate does not compile on newer rust version

I am using this crate in an osdev project, and after upgrading rustc form version 1.62.0-beta to 1.66.0-beta the lib stopped working.
Error dump:

error: const `impl` for trait `Bit` which is not marked with `#[const_trait]`
   --> /home/filip/.cargo/registry/src/github.com-1ecc6299db9ec823/const-bitfield-0.2.1/src/lib.rs:237:36
    |
226 | pub trait Bit {
    | - help: mark `Bit` as const: `#[const_trait]`
...
237 | impl<T: ~const BitRange<u8>> const Bit for T {
    |                                    ^^^
    |
    = note: marking a trait with `#[const_trait]` ensures all default method bodies are `const`
    = note: adding a non-const method body in the future would be a breaking change

error: ~const can only be applied to `#[const_trait]` traits
   --> /home/filip/.cargo/registry/src/github.com-1ecc6299db9ec823/const-bitfield-0.2.1/src/lib.rs:237:16
    |
237 | impl<T: ~const BitRange<u8>> const Bit for T {
    |                ^^^^^^^^^^^^

error: const `impl` for trait `BitMut` which is not marked with `#[const_trait]`
   --> /home/filip/.cargo/registry/src/github.com-1ecc6299db9ec823/const-bitfield-0.2.1/src/lib.rs:243:61
    |
232 | pub trait BitMut: Bit {
    | - help: mark `BitMut` as const: `#[const_trait]`
...
243 | impl<T: ~const BitRange<u8> + ~const BitRangeMut<u8>> const BitMut for T {
    |                                                             ^^^^^^
    |
    = note: marking a trait with `#[const_trait]` ensures all default method bodies are `const`
    = note: adding a non-const method body in the future would be a breaking change

error: ~const can only be applied to `#[const_trait]` traits
   --> /home/filip/.cargo/registry/src/github.com-1ecc6299db9ec823/const-bitfield-0.2.1/src/lib.rs:243:16
    |
243 | impl<T: ~const BitRange<u8> + ~const BitRangeMut<u8>> const BitMut for T {
    |                ^^^^^^^^^^^^

error: ~const can only be applied to `#[const_trait]` traits
   --> /home/filip/.cargo/registry/src/github.com-1ecc6299db9ec823/const-bitfield-0.2.1/src/lib.rs:243:38
    |
243 | impl<T: ~const BitRange<u8> + ~const BitRangeMut<u8>> const BitMut for T {
    |                                      ^^^^^^^^^^^^^^^

error: const `impl` for trait `BitRange` which is not marked with `#[const_trait]`
   --> /home/filip/.cargo/registry/src/github.com-1ecc6299db9ec823/const-bitfield-0.2.1/src/lib.rs:263:20
    |
214 | pub trait BitRange<V> {
    | - help: mark `BitRange` as const: `#[const_trait]`
...
263 |         impl const BitRange<$range_type> for $storage_type {
    |                    ^^^^^^^^^^^^^^^^^^^^^
...
317 | impl_bitrange! {uint, (u8, u16, u32, u64, u128), (u8, u16, u32, u64, u128)}
    | --------------------------------------------------------------------------- in this macro invocation
    |
    = note: marking a trait with `#[const_trait]` ensures all default method bodies are `const`
    = note: adding a non-const method body in the future would be a breaking change
    = note: this error originates in the macro `impl_bitrange` (in Nightly builds, run with -Z macro-backtrace for more info)

error: const `impl` for trait `BitRangeMut` which is not marked with `#[const_trait]`
   --> /home/filip/.cargo/registry/src/github.com-1ecc6299db9ec823/const-bitfield-0.2.1/src/lib.rs:284:20
    |
220 | pub trait BitRangeMut<V>: BitRange<V> {
    | - help: mark `BitRangeMut` as const: `#[const_trait]`
...
284 |         impl const BitRangeMut<$range_type> for $storage_type {
    |                    ^^^^^^^^^^^^^^^^^^^^^^^^
...
317 | impl_bitrange! {uint, (u8, u16, u32, u64, u128), (u8, u16, u32, u64, u128)}
    | --------------------------------------------------------------------------- in this macro invocation
    |
    = note: marking a trait with `#[const_trait]` ensures all default method bodies are `const`
    = note: adding a non-const method body in the future would be a breaking change
    = note: this error originates in the macro `impl_bitrange` (in Nightly builds, run with -Z macro-backtrace for more info)

error: const `impl` for trait `BitRange` which is not marked with `#[const_trait]`
   --> /home/filip/.cargo/registry/src/github.com-1ecc6299db9ec823/const-bitfield-0.2.1/src/lib.rs:263:20
    |
214 | pub trait BitRange<V> {
    | - help: mark `BitRange` as const: `#[const_trait]`
...
263 |         impl const BitRange<$range_type> for $storage_type {
    |                    ^^^^^^^^^^^^^^^^^^^^^
...
318 | impl_bitrange! {uint, (u8, u16, u32, u64, u128), (i8, i16, i32, i64, i128)}
    | --------------------------------------------------------------------------- in this macro invocation
    |
    = note: marking a trait with `#[const_trait]` ensures all default method bodies are `const`
    = note: adding a non-const method body in the future would be a breaking change
    = note: this error originates in the macro `impl_bitrange` (in Nightly builds, run with -Z macro-backtrace for more info)

error: const `impl` for trait `BitRangeMut` which is not marked with `#[const_trait]`
   --> /home/filip/.cargo/registry/src/github.com-1ecc6299db9ec823/const-bitfield-0.2.1/src/lib.rs:284:20
    |
220 | pub trait BitRangeMut<V>: BitRange<V> {
    | - help: mark `BitRangeMut` as const: `#[const_trait]`
...
284 |         impl const BitRangeMut<$range_type> for $storage_type {
    |                    ^^^^^^^^^^^^^^^^^^^^^^^^
...
318 | impl_bitrange! {uint, (u8, u16, u32, u64, u128), (i8, i16, i32, i64, i128)}
    | --------------------------------------------------------------------------- in this macro invocation
    |
    = note: marking a trait with `#[const_trait]` ensures all default method bodies are `const`
    = note: adding a non-const method body in the future would be a breaking change
    = note: this error originates in the macro `impl_bitrange` (in Nightly builds, run with -Z macro-backtrace for more info)

error: could not compile `const-bitfield` due to 105 previous errors

@ppmathis Are you still maintaining this project or should i find a different solution?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.