Skip to content

Commit

Permalink
std.IndexedSet.iterator: allow iteration on const EnumSet
Browse files Browse the repository at this point in the history
  • Loading branch information
skabbes authored Dec 17, 2022
1 parent 270b6c4 commit 90477e5
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion lib/std/enums.zig
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ pub fn IndexedSet(comptime I: type, comptime Ext: fn (type) type) type {
/// index order. Modifications to the set during iteration
/// may or may not be observed by the iterator, but will
/// not invalidate it.
pub fn iterator(self: *Self) Iterator {
pub fn iterator(self: *const Self) Iterator {
return .{ .inner = self.bits.iterator(.{}) };
}

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

test "std.enums.EnumSet const iterator" {
const Direction = enum { up, down, left, right };
const diag_move = init: {
var move = EnumSet(Direction).initEmpty();
move.insert(.right);
move.insert(.up);
break :init move;
};

var result = EnumSet(Direction).initEmpty();
var it = diag_move.iterator();
while (it.next()) |dir| {
result.insert(dir);
}

try testing.expect(result.eql(diag_move));
}

/// A map from keys to values, using an index lookup. Uses a
/// bitfield to track presence and a dense array of values.
/// This type does no allocation and can be copied by value.
Expand Down

0 comments on commit 90477e5

Please sign in to comment.