Add `std.heap.static_allocator`
An allocator with a always failing alloc and resize, and a no-op free, for use with a statically allocated buffer being passed into an api that takes an Allocator in order to free its data.
The idea of this API is to use for a structure that holds a reference to an object (most likely a string), conceptually owning it, but the data could also be static data.
So instead of :
union(enum) {
owned: struct { data: []const u8, allocator: Allocator },
static_data: []const u8,
}
Writing
struct {
data: []const u8,
allocator: Allocator, // can be static_allocator for static data
},
making the use of the struct much simpler.
How about renaming it to noop_allocator so it's clearer what it actually does? The intended usage would be part of the doc comment instead of the name.
yeah this is more like https://github.com/nektro/gimme/blob/master/src/FailingAllocator.zig
A "failing allocator" already exists as std.testing.failing_allocator.
It may be prudent to move failing_allocator to heap to clarify such a use case.
cc @IntegratedQuantum, you seemed discontent with this use case previously.
The idea of this API is to use for a structure that holds a reference to an object (most likely a string), conceptually owning it, but the data could also be static data.
So instead of :
union(enum) { owned: struct { data: []const u8, allocator: Allocator }, static_data: []const u8, }Writing
struct { data: []const u8, allocator: Allocator, // can be static_allocator for static data },making the use of the struct much simpler.
This could be easily handled using optionals and a simple if to decide whether to call free
struct {
data: []const u8,
allocator: ?Allocator, // when null `data` is static
}