I’ve got a project where I’m parsing headers from a binary file. So far I’ve been reading from static addresses like so:
self.page_size = std.mem.readInt(u16, buf[16..18], .big);
self.file_change_counter = std.mem.readInt(u32, buf[24..28], .big);
self.num_pages = std.mem.readInt(u32, buf[28..32], .big);
This has worked well. However now I need to parse a dynamic number of integers into a buffer, which I am doing like so:
while (i < self.page_header.num_cells) : (i += 1) {
const slice_start = start_idx + i * 2;
const slice_end = slice_start + 2;
cells[i] = std.mem.readInt(u16, self.page_buf[slice_start..slice_end], .big);
}
However this yields to a type error that I don’t really understand:
src/db.zig:63:58: error: expected type '*const [2]u8', found '[]u8'
cells[i] = std.mem.readInt(u16, self.page_buf[slice_start..slice_end], .big);
~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
/home/axelmagn/.zvm/master/lib/std/mem.zig:1797:49: note: parameter type declared here
pub inline fn readInt(comptime T: type, buffer: *const [@divExact(@typeInfo(T).int.bits, 8)]u8, endian: Endian) T {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I don’t understand:
- Why does this work for literal indices but not variable indices?
- Is it possible to coerce a slice into *const [2]u8?
- If so, how?