make Iterator(Inner).toOwnedSlice and Iterator(Inner).collectInto take pointer to self

This commit is contained in:
Luuk Machielse 2025-12-10 16:21:29 +01:00
parent 7ec0bf20dc
commit b9098aea59

View file

@ -51,14 +51,14 @@ test iter {
}
};
const items = try iter(i32)
var it = iter(i32)
.once(-100)
.chain(
iter(i32).rangeInclusive(1, 100)
.filter(arith.is_square)
.chain(iter(i32).range(0, 10)),
)
.toOwnedSlice(allocator);
iter(i32).rangeInclusive(1, 100)
.filter(arith.is_square)
.chain(iter(i32).range(0, 10)),
);
const items = try it.toOwnedSlice(allocator);
defer allocator.free(items);
try std.testing.expectEqualSlices(
@ -196,7 +196,7 @@ pub fn Iterator(comptime Inner: type) type {
try expectEqualDeep(null, it.next());
}
pub fn toOwnedSlice(self: Self, allocator: std.mem.Allocator) ![]const Item {
pub fn toOwnedSlice(self: *Self, allocator: std.mem.Allocator) ![]const Item {
var list: std.ArrayList(Item) = try .initCapacity(allocator, self.sizeHint());
errdefer list.deinit(allocator);
@ -205,9 +205,8 @@ pub fn Iterator(comptime Inner: type) type {
return try list.toOwnedSlice(allocator);
}
pub fn collectInto(self: Self, list: *std.ArrayList(Item), allocator: std.mem.Allocator) !void {
var mutSelf = self;
while (mutSelf.next()) |val| {
pub fn collectInto(self: *Self, list: *std.ArrayList(Item), allocator: std.mem.Allocator) !void {
while (self.next()) |val| {
try list.append(allocator, val);
}
}