authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-11-21 18:30:42-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-11-22 11:32:14-07:00
logf6549a956d640c1de2dc99340582950f9dc18c60
tree6d043ee95bac012b6c65dbdf5db8cc84dd0478a8
parentd5e21a4f1a2920ef7bbe3c54feab1a3b5119bf77

std.ArrayList: add initBuffer to the unmanaged array list

This is useful when you want to have an array list backed by a fixed slice of memory and no Allocator will be used. It's an alternative to BoundedArray as you will see in the following commit.

1 files changed, 11 insertions(+), 0 deletions(-)

lib/std/array_list.zig+11
...@@ -633,6 +633,17 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ...@@ -633,6 +633,17 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
633 return self;633 return self;
634 }634 }
635635
636 /// Initialize with externally-managed memory. The buffer determines the
637 /// capacity, and the length is set to zero.
638 /// When initialized this way, all methods that accept an Allocator
639 /// argument are illegal to call.
640 pub fn initBuffer(buffer: Slice) Self {
641 return .{
642 .items = buffer[0..0],
643 .capacity = buffer.len,
644 };
645 }
646
636 /// Release all allocated memory.647 /// Release all allocated memory.
637 pub fn deinit(self: *Self, allocator: Allocator) void {648 pub fn deinit(self: *Self, allocator: Allocator) void {
638 allocator.free(self.allocatedSlice());649 allocator.free(self.allocatedSlice());