mirror of
https://github.com/ziglang/zig.git
synced 2026-01-09 08:55:36 +00:00
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:
parent
599641357c
commit
c4ad6be002
@ -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);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user