authorgravatar for erik.arvstedt@gmail.comErik Arvstedt <erik.arvstedt@gmail.com> 2022-05-04 20:01:49+02:00
committergravatar for erik.arvstedt@gmail.comErik Arvstedt <erik.arvstedt@gmail.com> 2022-05-04 21:26:48+02:00
loge5b07d8187c8e36c7fad4d190d9eac604018ce3b
tree4b8b1e2ab07a7f68f6a4b8516e239d627d6abf30
parent8b8e57c670132d615f2b5bbf69acf1658c083b12

std.meta.TrailerFlags: improve init type


1 files changed, 8 insertions(+), 8 deletions(-)

lib/std/meta/trailer_flags.zig+8-8
......@@ -18,7 +18,8 @@ pub fn TrailerFlags(comptime Fields: type) type {
1818
1919 pub const FieldEnum = std.meta.FieldEnum(Fields);
2020
21 pub const InitStruct = blk: {
21 pub const ActiveFields = std.enums.EnumFieldStruct(FieldEnum, bool, false);
22 pub const FieldValues = blk: {
2223 comptime var fields: [bit_count]Type.StructField = undefined;
2324 inline for (@typeInfo(Fields).Struct.fields) |struct_field, i| {
2425 fields[i] = Type.StructField{
......@@ -57,19 +58,18 @@ pub fn TrailerFlags(comptime Fields: type) type {
5758 self.bits |= 1 << field_index;
5859 }
5960
60 /// `fields` is a struct with each field set to an optional value.
61 /// Only the non-null bits are observed and are used to set the flag bits.
62 pub fn init(fields: InitStruct) Self {
61 /// `fields` is a boolean struct where each active field is set to `true`
62 pub fn init(fields: ActiveFields) Self {
6363 var self: Self = .{ .bits = 0 };
6464 inline for (@typeInfo(Fields).Struct.fields) |field, i| {
65 if (@field(fields, field.name)) |_|
65 if (@field(fields, field.name))
6666 self.bits |= 1 << i;
6767 }
6868 return self;
6969 }
7070
71 /// `fields` is a struct with each field set to an optional value (same as `init`).
72 pub fn setMany(self: Self, p: [*]align(@alignOf(Fields)) u8, fields: InitStruct) void {
71 /// `fields` is a struct with each field set to an optional value
72 pub fn setMany(self: Self, p: [*]align(@alignOf(Fields)) u8, fields: FieldValues) void {
7373 inline for (@typeInfo(Fields).Struct.fields) |field, i| {
7474 if (@field(fields, field.name)) |value|
7575 self.set(p, @intToEnum(FieldEnum, i), value);
......@@ -142,7 +142,7 @@ test "TrailerFlags" {
142142
143143 var flags = Flags.init(.{
144144 .b = true,
145 .c = 1234,
145 .c = true,
146146 });
147147 const slice = try testing.allocator.allocAdvanced(u8, 8, flags.sizeInBytes(), .exact);
148148 defer testing.allocator.free(slice);