Hello! I have an old function thats been kicking around in my codebase since about zig 0.9 days and with there recent update to 0.15 the bitrot has gotten truly wild.
basically, what this function does is load in a file completely into memory at a specific alignment. I use this for some serialization functions.
pub fn loadFileAlloc(filename: []const u8, comptime alignment: std.mem.Alignment, allocator: std.mem.Allocator) ![]u8 {
var file = try std.fs.cwd().openFile(filename, .{});
defer file.close();
const filesize = (try file.stat()).size + 1; // add null byte
const buffer: []u8 = try allocator.alignedAlloc(u8, alignment, filesize);
errdefer allocator.free(buffer);
const fileReadBuffer = try file.readToEndAlloc(allocator, buffer.len - 1);
defer allocator.free(fileReadBuffer);
std.mem.copyForwards(u8, buffer, fileReadBuffer);
buffer[buffer.len - 1] = 0;
return buffer;
}
I suspect there must be a way to lay this function to rest with the new writers interface.