mirror of
https://codeberg.org/kaasboteram/zig-iterating.git
synced 2025-12-15 09:43:14 +01:00
make Iterator(Inner).toOwnedSlice and Iterator(Inner).collectInto take pointer to self
This commit is contained in:
parent
7ec0bf20dc
commit
b9098aea59
1 changed files with 9 additions and 10 deletions
19
src/root.zig
19
src/root.zig
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue