Skip to content

Commit 90477e5

Browse files
authored
std.IndexedSet.iterator: allow iteration on const EnumSet
1 parent 270b6c4 commit 90477e5

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

lib/std/enums.zig

+19-1
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ pub fn IndexedSet(comptime I: type, comptime Ext: fn (type) type) type {
878878
/// index order. Modifications to the set during iteration
879879
/// may or may not be observed by the iterator, but will
880880
/// not invalidate it.
881-
pub fn iterator(self: *Self) Iterator {
881+
pub fn iterator(self: *const Self) Iterator {
882882
return .{ .inner = self.bits.iterator(.{}) };
883883
}
884884

@@ -970,6 +970,24 @@ test "pure EnumSet fns" {
970970
try testing.expect(full.differenceWith(black).eql(red));
971971
}
972972

973+
test "std.enums.EnumSet const iterator" {
974+
const Direction = enum { up, down, left, right };
975+
const diag_move = init: {
976+
var move = EnumSet(Direction).initEmpty();
977+
move.insert(.right);
978+
move.insert(.up);
979+
break :init move;
980+
};
981+
982+
var result = EnumSet(Direction).initEmpty();
983+
var it = diag_move.iterator();
984+
while (it.next()) |dir| {
985+
result.insert(dir);
986+
}
987+
988+
try testing.expect(result.eql(diag_move));
989+
}
990+
973991
/// A map from keys to values, using an index lookup. Uses a
974992
/// bitfield to track presence and a dense array of values.
975993
/// This type does no allocation and can be copied by value.

0 commit comments

Comments
 (0)