Allow empty enum to be used in EnumSet/EnumMap

Moves the check for empty fields before any access to those fields.
This commit is contained in:
Christofer Nolander 2023-09-28 23:48:39 +02:00 committed by GitHub
parent 599641357c
commit c4ad6be002
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -980,6 +980,17 @@ test "pure EnumSet fns" {
try testing.expect(full.differenceWith(black).eql(red));
}
test "std.enums.EnumSet empty" {
const E = enum {};
const empty = EnumSet(E).initEmpty();
const full = EnumSet(E).initFull();
try std.testing.expect(empty.eql(full));
try std.testing.expect(empty.complement().eql(full));
try std.testing.expect(empty.complement().eql(full.complement()));
try std.testing.expect(empty.eql(full.complement()));
}
test "std.enums.EnumSet const iterator" {
const Direction = enum { up, down, left, right };
const diag_move = init: {
@ -1296,9 +1307,8 @@ pub fn EnumIndexer(comptime E: type) type {
const const_fields = std.meta.fields(E);
var fields = const_fields[0..const_fields.len].*;
const min = fields[0].value;
const max = fields[fields.len - 1].value;
const fields_len = fields.len;
if (fields_len == 0) {
return struct {
pub const Key = E;
@ -1314,6 +1324,9 @@ pub fn EnumIndexer(comptime E: type) type {
};
}
const min = fields[0].value;
const max = fields[fields.len - 1].value;
const SortContext = struct {
fields: []EnumField,
@ -1424,3 +1437,11 @@ test "std.enums.EnumIndexer sparse" {
try testing.expectEqual(E.b, Indexer.keyForIndex(1));
try testing.expectEqual(E.c, Indexer.keyForIndex(2));
}
test "std.enums.EnumIndexer empty" {
const E = enum {};
const Indexer = EnumIndexer(E);
ensureIndexer(Indexer);
try testing.expectEqual(E, Indexer.Key);
try testing.expectEqual(@as(usize, 0), Indexer.count);
}