Takes a sentinel-terminated pointer and returns a slice preserving pointer attributes.
[*c] pointers are assumed to be 0-terminated and assumed to not be allowzero.
fn Span(comptime T: type) type
fn Span(comptime T: type) type {
switch (@typeInfo(T)) {
.optional => |optional_info| {
return ?Span(optional_info.child);
},
.pointer => |ptr_info| {
const new_sentinel: ?ptr_info.child = switch (ptr_info.size) {
.one, .slice => @compileError("invalid type given to std.mem.span: " ++ @typeName(T)),
.many => ptr_info.sentinel() orelse @compileError("invalid type given to std.mem.span: " ++ @typeName(T)),
.c => 0,
};
var attrs = ptr_info.attrs;
attrs.@"allowzero" = attrs.@"allowzero" and ptr_info.size != .c;
return @Pointer(.slice, attrs, ptr_info.child, new_sentinel);
},
else => {},
}
@compileError("invalid type given to std.mem.span: " ++ @typeName(T));
}