authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-05-19 21:15:16-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-05-19 21:15:16-04:00
logdd6ac9a22a48f048fa619aeca95d8e10ec24829d
treed40e1c99906324096176cb5010172709176b77c4
parent85a9bd932f5fd77afecf2443a291f82f0f4a6eaa
parente5b07d8187c8e36c7fad4d190d9eac604018ce3b
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #11581 from erikarvstedt/fix-trailerflags

Fix `std.meta.TrailerFlags`

2 files changed, 13 insertions(+), 12 deletions(-)

lib/std/meta.zig+4
......@@ -10,6 +10,10 @@ pub const TrailerFlags = @import("meta/trailer_flags.zig").TrailerFlags;
1010
1111const Type = std.builtin.Type;
1212
13test "std.meta.TrailerFlags" {
14 _ = TrailerFlags;
15}
16
1317pub fn tagName(v: anytype) []const u8 {
1418 const T = @TypeOf(v);
1519 switch (@typeInfo(T)) {
lib/std/meta/trailer_flags.zig+9-12
......@@ -18,16 +18,14 @@ 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{
2526 .name = struct_field.name,
2627 .field_type = ?struct_field.field_type,
27 .default_value = @as(
28 ??struct_field.field_type,
29 @as(?struct_field.field_type, null),
30 ),
28 .default_value = &@as(?struct_field.field_type, null),
3129 .is_comptime = false,
3230 .alignment = @alignOf(?struct_field.field_type),
3331 };
......@@ -60,19 +58,18 @@ pub fn TrailerFlags(comptime Fields: type) type {
6058 self.bits |= 1 << field_index;
6159 }
6260
63 /// `fields` is a struct with each field set to an optional value.
64 /// Only the non-null bits are observed and are used to set the flag bits.
65 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 {
6663 var self: Self = .{ .bits = 0 };
6764 inline for (@typeInfo(Fields).Struct.fields) |field, i| {
68 if (@field(fields, field.name)) |_|
65 if (@field(fields, field.name))
6966 self.bits |= 1 << i;
7067 }
7168 return self;
7269 }
7370
74 /// `fields` is a struct with each field set to an optional value (same as `init`).
75 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 {
7673 inline for (@typeInfo(Fields).Struct.fields) |field, i| {
7774 if (@field(fields, field.name)) |value|
7875 self.set(p, @intToEnum(FieldEnum, i), value);
......@@ -145,7 +142,7 @@ test "TrailerFlags" {
145142
146143 var flags = Flags.init(.{
147144 .b = true,
148 .c = 1234,
145 .c = true,
149146 });
150147 const slice = try testing.allocator.allocAdvanced(u8, 8, flags.sizeInBytes(), .exact);
151148 defer testing.allocator.free(slice);