authorgravatar for stevenkabbes@gmail.comSteven Kabbes <stevenkabbes@gmail.com> 2022-12-17 02:37:03-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-12-17 11:37:03+02:00
log90477e5c10c9c263a3c0038e1ae7b814d2c5397e
treefe8d59806076fb2713f76c7a0fc23a697cc1d5fb
parent270b6c4c2f28c26576422d0f8d334762e7efdba9
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

std.IndexedSet.iterator: allow iteration on const EnumSet


1 files changed, 19 insertions(+), 1 deletions(-)

lib/std/enums.zig+19-1
......@@ -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
973test "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.