D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4425 - More bells & whistles for bitfields
Summary: More bells & whistles for bitfields
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: Era Scarecrow
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-07-04 12:39 UTC by bearophile_hugs
Modified: 2021-01-09 22:39 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2010-07-04 12:39:29 UTC
When bit fields are used to define bit protocols etc., they sometimes have constant fields, or already initialized variable fields. So It can be positive for std.bitmanip.bitfields to support that too, this is an example:

          field1 : 1;
          field2 : 2 = 0b01;
immutable field3 : 3;
immutable field4 : 4 = 0b1110;


As with normal immutable fields in structs, the language has to forbid the write on immutable fields. Probably in many cases the compiler can't enforce this at compile time on bit fields (because of the type system granularity; something similar happens with pointers to single bits in bit-arrays), so probably the bit field immutability needs to be enforced at run-time (maybe even in release mode too, because it's a hard constraint).
Comment 1 bearophile_hugs 2010-09-24 18:47:19 UTC
See also bug 4937
Comment 2 bearophile_hugs 2010-10-04 18:16:44 UTC
Another possible feature is to make std.bitmanip.bitfields generate two versions of the code, that get compiled conditionally according to the CPU endianess:


static if (std.system.endian == std.system.Endian.BigEndian) {
    ...
} else {
    ...
}
Comment 3 Era Scarecrow 2012-08-02 22:02:41 UTC
Adding const to a field seems pretty easy; specifically the setter is simply left out of the template. So as a final proposed usage, it would look like:

 mixin(bitfields(
          int,  "named",           4,
    const int,  "c_named",         4,
          uint, "named_def=5",     4,    //includes defaulted value of 5
    const uint, "c_named_def=10",  4));  //defaulted value of 10


 bitfieldsOn is being worked on, and effectively works identically to bitfields except you specify a variable you want to use; Default values won't work well with it however.
Comment 4 bearophile_hugs 2012-08-03 03:06:07 UTC
(In reply to comment #3)
> Adding const to a field seems pretty easy; specifically the setter is simply
> left out of the template.

OK.


> So as a final proposed usage, it would look like:
> 
>  mixin(bitfields(
>           int,  "named",           4,
>     const int,  "c_named",         4,
>           uint, "named_def=5",     4,    //includes defaulted value of 5
>     const uint, "c_named_def=10",  4));  //defaulted value of 10

Good. And what do you think about facing the endianess problems (Comment 2)?


>  bitfieldsOn is being worked on, and effectively works identically to bitfields
> except you specify a variable you want to use; Default values won't work well
> with it however.

What's bitfieldsOn?
Comment 5 Mathias LANG 2021-01-09 22:39:15 UTC
I don't see any actionable item here, just some ideas.
The situation have changed quite a bit since this issue was created: In particular, `static immutable` field do not occupy space in aggregates anymore, so  the idea of having a CT-known `immutable` field means that it would likely be decoupled from the runtime one. As a result, closing as WONTFIX.