Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add #[repr = uN] annotation support for #[bitfield] #33

Closed
Robbepop opened this issue Oct 26, 2020 · 0 comments · Fixed by #38
Closed

Add #[repr = uN] annotation support for #[bitfield] #33

Robbepop opened this issue Oct 26, 2020 · 0 comments · Fixed by #38
Labels
enhancement New feature or request

Comments

@Robbepop
Copy link
Owner

Robbepop commented Oct 26, 2020

When given a #[bitfield] struct with a #[repr = uN], e.g. #[repr = u32] attribute the #[bitfield] macro shall make sure that the generated bitfield struct respects the bitwidth of the uN, e.g. 8 bits for u8, 16 bits for u16, etc.

Also it shall generate From<uN> and From<BitfieldStruct> for uN implementations.
Obviously having #[repr(uN)] also changes the underlying representation of the bitfield type to uN.

Example

#[bitfield]
#[repr(u32)]
struct TtResp {
    mregion: u8,
    sregion: u8, 
    mrvalid: bool,
    srvalid: bool,
    r: bool,
    rw: bool,
    nsr: bool,
    nsrw: bool,
    s: bool,
    irvalid: bool,
    iregion: u8,
}

This allows the user to only conditionally have the repr(u32) effects taken place using cfg_attr:

#[bitfield]
#[cfg_attr(test, repr(u32))]
struct TtResp {
    mregion: u8,
    sregion: u8, 
    mrvalid: bool,
    srvalid: bool,
    r: bool,
    rw: bool,
    nsr: bool,
    nsrw: bool,
    s: bool,
    irvalid: bool,
    iregion: u8,
}
@Robbepop Robbepop added the enhancement New feature or request label Oct 26, 2020
Robbepop added a commit that referenced this issue Oct 27, 2020
* [tests] write tests for bytes=N #[bitfield] parameter

* [tests] fix test case assertions

* [tests] add some new failure tests for bytes parameter

* [impl] implement bytes parameter parsing and checking

* [tests] add test for duplicate bytes parameter

* [impl] improve span for duplicate bytes parameter

* [tests] add expected failure output for tests #33 and #34

* add implementation of new bytes=N check

* [tests] move bytes=N tests into their own directory

* [impl] apply rustfmt

* add impl sub-crate to workspace of modular_bitfield crate

* adjust GitHub actions CI to workspace
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant