authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-05-26 12:33:42-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-05-26 16:13:54-04:00
logb9b7f1852308cf41bef511b9ccb3e24e353bf109
tree3c7fb40a84f83679dcb48b2f37aa689c4b6303b7
parent6e5e7e7b1966632399a942f975af23401d3e2137

EnumMap: fix init


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

lib/std/enums.zig+14-1
...@@ -452,7 +452,7 @@ pub fn EnumMap(comptime E: type, comptime V: type) type {...@@ -452,7 +452,7 @@ pub fn EnumMap(comptime E: type, comptime V: type) type {
452 values: [Indexer.count]Value = undefined,452 values: [Indexer.count]Value = undefined,
453453
454 /// Initializes the map using a sparse struct of optionals454 /// Initializes the map using a sparse struct of optionals
455 pub fn init(init_values: EnumFieldStruct(E, ?Value, null)) Self {455 pub fn init(init_values: EnumFieldStruct(E, ?Value, @as(?Value, null))) Self {
456 @setEvalBranchQuota(2 * @typeInfo(E).Enum.fields.len);456 @setEvalBranchQuota(2 * @typeInfo(E).Enum.fields.len);
457 var result: Self = .{};457 var result: Self = .{};
458 if (@typeInfo(E).Enum.is_exhaustive) {458 if (@typeInfo(E).Enum.is_exhaustive) {
...@@ -652,6 +652,19 @@ pub fn EnumMap(comptime E: type, comptime V: type) type {...@@ -652,6 +652,19 @@ pub fn EnumMap(comptime E: type, comptime V: type) type {
652 };652 };
653}653}
654654
655test EnumMap {
656 const Ball = enum { red, green, blue };
657
658 const some = EnumMap(Ball, u8).init(.{
659 .green = 0xff,
660 .blue = 0x80,
661 });
662 try testing.expectEqual(2, some.count());
663 try testing.expectEqual(null, some.get(.red));
664 try testing.expectEqual(0xff, some.get(.green));
665 try testing.expectEqual(0x80, some.get(.blue));
666}
667
655/// A multiset of enum elements up to a count of usize. Backed668/// A multiset of enum elements up to a count of usize. Backed
656/// by an EnumArray. This type does no dynamic allocation and can669/// by an EnumArray. This type does no dynamic allocation and can
657/// be copied by value.670/// be copied by value.