EnumMap: fix init

This commit is contained in:
Jacob Young 2024-05-26 12:33:42 -04:00 committed by Andrew Kelley
parent 2d4b264527
commit 6e469bc44d

View File

@ -452,7 +452,7 @@ pub fn EnumMap(comptime E: type, comptime V: type) type {
values: [Indexer.count]Value = undefined, values: [Indexer.count]Value = undefined,
/// Initializes the map using a sparse struct of optionals /// Initializes the map using a sparse struct of optionals
pub fn init(init_values: EnumFieldStruct(E, ?Value, null)) Self { pub fn init(init_values: EnumFieldStruct(E, ?Value, @as(?Value, null))) Self {
@setEvalBranchQuota(2 * @typeInfo(E).Enum.fields.len); @setEvalBranchQuota(2 * @typeInfo(E).Enum.fields.len);
var result: Self = .{}; var result: Self = .{};
if (@typeInfo(E).Enum.is_exhaustive) { if (@typeInfo(E).Enum.is_exhaustive) {
@ -652,6 +652,19 @@ pub fn EnumMap(comptime E: type, comptime V: type) type {
}; };
} }
test EnumMap {
const Ball = enum { red, green, blue };
const some = EnumMap(Ball, u8).init(.{
.green = 0xff,
.blue = 0x80,
});
try testing.expectEqual(2, some.count());
try testing.expectEqual(null, some.get(.red));
try testing.expectEqual(0xff, some.get(.green));
try testing.expectEqual(0x80, some.get(.blue));
}
/// A multiset of enum elements up to a count of usize. Backed /// A multiset of enum elements up to a count of usize. Backed
/// by an EnumArray. This type does no dynamic allocation and can /// by an EnumArray. This type does no dynamic allocation and can
/// be copied by value. /// be copied by value.