commit b9b7f1852308cf41bef511b9ccb3e24e353bf109 (tree)
parent 6e5e7e7b1966632399a942f975af23401d3e2137
Author: Jacob Young <jacobly0@users.noreply.github.com>
Date: Sun, 26 May 2024 12:33:42 -0400
EnumMap: fix init
Diffstat:
1 file changed, 14 insertions(+), 1 deletion(-)
diff --git a/lib/std/enums.zig b/lib/std/enums.zig
@@ -452,7 +452,7 @@ pub fn EnumMap(comptime E: type, comptime V: type) type {
values: [Indexer.count]Value = undefined,
/// 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);
var result: Self = .{};
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
/// by an EnumArray. This type does no dynamic allocation and can
/// be copied by value.