| ... | @@ -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, |
| 453 | | 453 | |
| 454 | /// Initializes the map using a sparse struct of optionals | 454 | /// 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 | } |
| 654 | | 654 | |
| | 655 | test 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. Backed | 668 | /// A multiset of enum elements up to a count of usize. Backed |
| 656 | /// by an EnumArray. This type does no dynamic allocation and can | 669 | /// by an EnumArray. This type does no dynamic allocation and can |
| 657 | /// be copied by value. | 670 | /// be copied by value. |