I want to iterate over an array via a C pointer until I find a null sentinel. However, my array is not comprised of primitives, but instead structs. I couldn't get std.mem.span to work with structs, so I've written this that seems to work:
fn spanMem(comptime T: type, ptr: [*c]const T) []const T {
const sentinel: [@sizeOf(T)]u8 = @bitCast(std.mem.zeroes(T));
var i: usize = 0;
while (true) : (i += 1) {
const elem: [@sizeOf(T)]u8 = @bitCast(ptr[i]);
if (std.mem.eql(u8, &elem, &sentinel))
return ptr[0..i];
}
}
Is this sensible? Is there a function in the std that I'm missing that will do this for me?