danlehmann / bitfield

Rust crate for bitfields and bit-enums
MIT License
46 stars 9 forks source link

Support private bitfields #57

Open vhu43 opened 4 months ago

vhu43 commented 4 months ago

Sorry, new to rust, and I might have missed something, but would it be possible to add private bitfields if they don't already exist?

pub mod foo {
    #[bitfield(u8, default = 0x00)]
    pub struct bar {
        #[bit(0, r)]
        pub cool: bool,

        #[bits(1..=4, r)]
        hotness: u4, // I want this to be private

        #[bits(5..=7, r)]
        pub weirdness: u3,
    }

    impl bar {
        pub fn hotness(&self) -> u8 {
            8 * self.hotness().value() // So hotness can be a multiple of 8
        }
    }
}
danlehmann commented 3 months ago

Sorry for the late response.

I think if I were to start over, that's probably how I would design this nowadays. But given that everything's public right, I am not sure how we could migrate to it as it's quite an API change.