mirror of
https://github.com/ziglang/zig.git
synced 2026-02-15 05:48:31 +00:00
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.
This commit is contained in:
parent
d5e21a4f1a
commit
f6549a956d
@ -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());
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user