bitstructs

Crates.iobitstructs
lib.rsbitstructs
version0.2.1
created_at2025-02-23 18:59:22.894493+00
updated_at2025-03-16 16:10:58.394959+00
descriptionDefining type-safe bitfield structures that can be used in both `std` and `no_std` environments.
homepage
repositoryhttps://github.com/small-white0-0/bitstructs
max_upload_size
id1566576
size35,996
(small-white0-0)

documentation

README

Bitstructs

bitstructs is a library for defining type-safe bitfield structs that can be used in both std and no_std environments.

Features

  • Type-safe bit operations
  • Simple to use and highly flexible
  • Supports both no_std and std
  • Implemented using proc-macros with no additional runtime code

Example

Here is a simple example of using bitstructs.

use bitstructs::{bitstruct_cow};
bitstruct_cow! {
    #[derive(Clone, Debug, PartialEq)]
    pub struct Test {
        a: 1,
        b: 2,
        c: 2 =>
        pub enum C {
            C0 = 0,
            C1 = 1,
        },
    }
}
let mut value = Test::new();
assert_eq!(value.as_bytes(), vec![0b0000_0000]);
value.set_a(true);
unsafe { value.set_b(1) };
value.set_c(C::C1);
assert_eq!(value.as_bytes(), vec![0b0000_1011]);

By default, the library uses LSB0 and little-endian mode. To use MSB0 and big-endian mode, add #[bitstruct_repr(MSB0)] to the struct.

use bitstructs::{bitstruct_cow};
bitstruct_cow! {
    /// MSB0 and big-endian
    #[derive(Clone, Debug, PartialEq)]
    #[bitstruct_repr(MSB0)]
    pub struct Test {
        a: 1,
        b: 2,
        c: 2 =>
        pub enum C {
            C0 = 0,
            C1 = 1,
        },
    }
}
let mut value = Test::new();
assert_eq!(value.to_bytes(), vec![0b0000_0000]);
value.set_a(true);
unsafe { value.set_b(1) };
value.set_c(C::C1);
assert_eq!(value.to_bytes(), vec![0b1010_1000]);

Documentation

For more information, please refer to the documentation.

Contribution

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

Commit count: 39

cargo fmt