diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2023-11-21 18:30:42 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2023-11-22 11:32:14 -0700 |
| commit | f6549a956d640c1de2dc99340582950f9dc18c60 (patch) | |
| tree | 6d043ee95bac012b6c65dbdf5db8cc84dd0478a8 /lib/std/array_list.zig | |
| parent | d5e21a4f1a2920ef7bbe3c54feab1a3b5119bf77 (diff) | |
| download | zig-f6549a956d640c1de2dc99340582950f9dc18c60.tar.gz zig-f6549a956d640c1de2dc99340582950f9dc18c60.zip | |
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.
Diffstat (limited to 'lib/std/array_list.zig')
| -rw-r--r-- | lib/std/array_list.zig | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/lib/std/array_list.zig b/lib/std/array_list.zig index 24655046a9..570c65ae99 100644 --- a/lib/std/array_list.zig +++ b/lib/std/array_list.zig @@ -633,6 +633,17 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ return self; } + /// Initialize with externally-managed memory. The buffer determines the + /// capacity, and the length is set to zero. + /// When initialized this way, all methods that accept an Allocator + /// argument are illegal to call. + pub fn initBuffer(buffer: Slice) Self { + return .{ + .items = buffer[0..0], + .capacity = buffer.len, + }; + } + /// Release all allocated memory. pub fn deinit(self: *Self, allocator: Allocator) void { allocator.free(self.allocatedSlice()); |
