diff options
| author | Erik Arvstedt <erik.arvstedt@gmail.com> | 2022-05-04 20:01:49 +0200 |
|---|---|---|
| committer | Erik Arvstedt <erik.arvstedt@gmail.com> | 2022-05-04 21:26:48 +0200 |
| commit | e5b07d8187c8e36c7fad4d190d9eac604018ce3b (patch) | |
| tree | 4b8b1e2ab07a7f68f6a4b8516e239d627d6abf30 /lib/std/meta | |
| parent | 8b8e57c670132d615f2b5bbf69acf1658c083b12 (diff) | |
| download | zig-e5b07d8187c8e36c7fad4d190d9eac604018ce3b.tar.gz zig-e5b07d8187c8e36c7fad4d190d9eac604018ce3b.zip | |
std.meta.TrailerFlags: improve init type
Diffstat (limited to 'lib/std/meta')
| -rw-r--r-- | lib/std/meta/trailer_flags.zig | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/lib/std/meta/trailer_flags.zig b/lib/std/meta/trailer_flags.zig index e4288692b1..212a47bc29 100644 --- a/lib/std/meta/trailer_flags.zig +++ b/lib/std/meta/trailer_flags.zig @@ -18,7 +18,8 @@ pub fn TrailerFlags(comptime Fields: type) type { pub const FieldEnum = std.meta.FieldEnum(Fields); - pub const InitStruct = blk: { + pub const ActiveFields = std.enums.EnumFieldStruct(FieldEnum, bool, false); + pub const FieldValues = blk: { comptime var fields: [bit_count]Type.StructField = undefined; inline for (@typeInfo(Fields).Struct.fields) |struct_field, i| { fields[i] = Type.StructField{ @@ -57,19 +58,18 @@ pub fn TrailerFlags(comptime Fields: type) type { self.bits |= 1 << field_index; } - /// `fields` is a struct with each field set to an optional value. - /// Only the non-null bits are observed and are used to set the flag bits. - pub fn init(fields: InitStruct) Self { + /// `fields` is a boolean struct where each active field is set to `true` + pub fn init(fields: ActiveFields) Self { var self: Self = .{ .bits = 0 }; inline for (@typeInfo(Fields).Struct.fields) |field, i| { - if (@field(fields, field.name)) |_| + if (@field(fields, field.name)) self.bits |= 1 << i; } return self; } - /// `fields` is a struct with each field set to an optional value (same as `init`). - pub fn setMany(self: Self, p: [*]align(@alignOf(Fields)) u8, fields: InitStruct) void { + /// `fields` is a struct with each field set to an optional value + pub fn setMany(self: Self, p: [*]align(@alignOf(Fields)) u8, fields: FieldValues) void { inline for (@typeInfo(Fields).Struct.fields) |field, i| { if (@field(fields, field.name)) |value| self.set(p, @intToEnum(FieldEnum, i), value); @@ -142,7 +142,7 @@ test "TrailerFlags" { var flags = Flags.init(.{ .b = true, - .c = 1234, + .c = true, }); const slice = try testing.allocator.allocAdvanced(u8, 8, flags.sizeInBytes(), .exact); defer testing.allocator.free(slice); |
