Comptime allocator provides a standard allocation interface for compile time allocations
tested on 0.12.0-dev.1769+bf5ab5451 main.zig
constcomptime_allocator=@import("comptime_allocator.zig");
pubfnmain() !void {
comptimevara: *i32=undefined;
comptime {
a=trycomptime_allocator.allocator.create(i32);
a.*=42;
}
std.debug.print("{}\n", .{a});
}constcomptime_allocator=@import("comptime_allocator");
pubfnmain() !void {
consts=comptimeblk: {
constallocator=comptime_allocator.allocator;
varlist=std.ArrayList(u8).init(allocator);
trylist.appendSlice("Hello ");
trylist.appendSlice("world");
break :blktrylist.toOwnedSlice();
};
std.debug.print("{any}\n", .{s});
}for more examples look into tests in src/comptime_allocator.zig
The only structures I managed to make it work for are std.ArrayList and parsing json, now that doesn't mean it doesn't work for the others, but I woudn't hold my breath. You can try make it work, but most problems are inherent to how zig works and std implementation, but if you find other structures it works on, I would like to know about it.
It doesn't work for std.HashMap
std.ArrayList, even when using comptime_allocator isn't possible to use with comptime types like
comptime_int or type (solution for this would be to create a special comptime ArrayList that such types
supports, if possible).