authorgravatar for git@scottredig.comScott Redig <git@scottredig.com> 2026-09-11 00:54:56-07:00
committergravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2026-09-12 09:28:55+02:00
log2be592bb3ca71621422428ec8874b34a77606013
treebf8afb5423abeda1651bddc24bc28d618f22d4d8
parente90365cd51fdc261d4f5ad9fc53729127ca615a8

Add EnumMap.empty, deprecate default fields


1 files changed, 16 insertions(+), 5 deletions(-)

lib/std/enums.zig+16-5
...@@ -420,6 +420,8 @@ pub fn EnumSet(comptime E: type) type {...@@ -420,6 +420,8 @@ pub fn EnumSet(comptime E: type) type {
420/// If the enum is exhaustive but not dense, a mapping will be constructed from420/// If the enum is exhaustive but not dense, a mapping will be constructed from
421/// enum values to dense indices. This type does no dynamic421/// enum values to dense indices. This type does no dynamic
422/// allocation and can be copied by value.422/// allocation and can be copied by value.
423///
424/// Default initialization is deprecated; use .empty instead.
423pub fn EnumMap(comptime E: type, comptime V: type) type {425pub fn EnumMap(comptime E: type, comptime V: type) type {
424 return struct {426 return struct {
425 const Self = @This();427 const Self = @This();
...@@ -441,6 +443,11 @@ pub fn EnumMap(comptime E: type, comptime V: type) type {...@@ -441,6 +443,11 @@ pub fn EnumMap(comptime E: type, comptime V: type) type {
441 /// bit is zero, the value is undefined.443 /// bit is zero, the value is undefined.
442 values: [Indexer.count]Value = undefined,444 values: [Indexer.count]Value = undefined,
443445
446 pub const empty: Self = .{
447 .bits = .empty,
448 .values = undefined,
449 };
450
444 /// Initializes the map using a sparse struct of optionals451 /// Initializes the map using a sparse struct of optionals
445 pub fn init(init_values: EnumFieldStruct(E, ?Value, @as(?Value, null))) Self {452 pub fn init(init_values: EnumFieldStruct(E, ?Value, @as(?Value, null))) Self {
446 @setEvalBranchQuota(2 * @typeInfo(E).@"enum".field_names.len);453 @setEvalBranchQuota(2 * @typeInfo(E).@"enum".field_names.len);
...@@ -470,12 +477,10 @@ pub fn EnumMap(comptime E: type, comptime V: type) type {...@@ -470,12 +477,10 @@ pub fn EnumMap(comptime E: type, comptime V: type) type {
470 /// Initializes a full mapping with all keys set to value.477 /// Initializes a full mapping with all keys set to value.
471 /// Consider using EnumArray instead if the map will remain full.478 /// Consider using EnumArray instead if the map will remain full.
472 pub fn initFull(value: Value) Self {479 pub fn initFull(value: Value) Self {
473 var result: Self = .{480 return .{
474 .bits = .full,481 .bits = .full,
475 .values = undefined,482 .values = @splat(value),
476 };483 };
477 @memset(&result.values, value);
478 return result;
479 }484 }
480485
481 /// Initializes a full mapping with supplied values.486 /// Initializes a full mapping with supplied values.
...@@ -645,7 +650,10 @@ pub fn EnumMap(comptime E: type, comptime V: type) type {...@@ -645,7 +650,10 @@ pub fn EnumMap(comptime E: type, comptime V: type) type {
645test EnumMap {650test EnumMap {
646 const Ball = enum { red, green, blue };651 const Ball = enum { red, green, blue };
647652
648 const some = EnumMap(Ball, u8).init(.{653 const none: EnumMap(Ball, u8) = .empty;
654 try testing.expectEqual(0, none.count());
655
656 const some: EnumMap(Ball, u8) = .init(.{
649 .green = 0xff,657 .green = 0xff,
650 .blue = 0x80,658 .blue = 0x80,
651 });659 });
...@@ -653,6 +661,9 @@ test EnumMap {...@@ -653,6 +661,9 @@ test EnumMap {
653 try testing.expectEqual(null, some.get(.red));661 try testing.expectEqual(null, some.get(.red));
654 try testing.expectEqual(0xff, some.get(.green));662 try testing.expectEqual(0xff, some.get(.green));
655 try testing.expectEqual(0x80, some.get(.blue));663 try testing.expectEqual(0x80, some.get(.blue));
664
665 const all: EnumMap(Ball, u8) = .initFull(0);
666 try testing.expectEqual(3, all.count());
656}667}
657668
658/// A multiset of enum elements up to a count of usize. Backed669/// A multiset of enum elements up to a count of usize. Backed