Skip to content

Commit

Permalink
Fix Issue 21628 - The padding bits of bitfields could be calculated a…
Browse files Browse the repository at this point in the history
…utomatically
  • Loading branch information
berni44 committed Feb 11, 2021
1 parent 81a968d commit 6c8bf04
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions std/bitmanip.d
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,18 @@ private template createStorageAndFields(Ts...)
{
enum Name = createStoreName!Ts;
enum Size = sizeOfBitField!Ts;
static if (Size == ubyte.sizeof * 8)
static if (Size <= ubyte.sizeof * 8)
alias StoreType = ubyte;
else static if (Size == ushort.sizeof * 8)
else static if (Size <= ushort.sizeof * 8)
alias StoreType = ushort;
else static if (Size == uint.sizeof * 8)
else static if (Size <= uint.sizeof * 8)
alias StoreType = uint;
else static if (Size == ulong.sizeof * 8)
else static if (Size <= ulong.sizeof * 8)
alias StoreType = ulong;
else
{
static assert(false, "Field widths must sum to 8, 16, 32, or 64");
import std.conv : to;
static assert(false, "Sum of field widths must be <= 64, is " ~ to!string(Size));
alias StoreType = ulong; // just to avoid another error msg
}
enum result
Expand Down Expand Up @@ -291,18 +292,17 @@ of the bitfields storage.
}

/**
The sum of all bit lengths in one $(D_PARAM bitfield) instantiation
must be exactly 8, 16, 32, or 64. If padding is needed, just allocate
one bitfield with an empty name.
In former versions of Phobos the sum of all bit lengths in one
$(D_PARAM bitfield) instantiation had to be of certain lengths.
This restriction has been dropped now.
*/
@safe unittest
{
struct A
{
mixin(bitfields!(
bool, "flag1", 1,
bool, "flag2", 1,
uint, "", 6));
bool, "flag2", 1));
}

A a;
Expand Down

0 comments on commit 6c8bf04

Please sign in to comment.