Function tags [src]
Given an enum or error set type, returns a pointer to an array containing all tags for that
enum or error set.
Prototype
pub fn tags(comptime T: type) *const [fields(T).len]T
Parameters
T: type
Example
test tags {
const E1 = enum { A, B };
const E2 = error{A};
const e1_tags = tags(E1);
const e2_tags = tags(E2);
try testing.expect(e1_tags.len == 2);
try testing.expectEqual(E1.A, e1_tags[0]);
try testing.expectEqual(E1.B, e1_tags[1]);
try testing.expect(e2_tags.len == 1);
try testing.expectEqual(E2.A, e2_tags[0]);
}
Source
pub fn tags(comptime T: type) *const [fields(T).len]T {
return comptime blk: {
const fieldInfos = fields(T);
var res: [fieldInfos.len]T = undefined;
for (fieldInfos, 0..) |field, i| {
res[i] = @field(T, field.name);
}
const final = res;
break :blk &final;
};
}