diff options
| author | daurnimator <quae@daurnimator.com> | 2019-12-27 14:31:20 +1100 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2020-03-01 13:04:29 -0500 |
| commit | 0b0de22fd1b0391f0cf1c7d821afc6522041c699 (patch) | |
| tree | f00dfb75125a1efa6cd3c6c5b5632e289c440f82 /lib/std/meta.zig | |
| parent | b99c6d56da3d2db995c268fb07f48548ea6d1148 (diff) | |
| download | zig-0b0de22fd1b0391f0cf1c7d821afc6522041c699.tar.gz zig-0b0de22fd1b0391f0cf1c7d821afc6522041c699.zip | |
std: format contents of sentinel terminated many pointers
std: add std.meta.Sentinel to get sentinel of a type
Diffstat (limited to 'lib/std/meta.zig')
| -rw-r--r-- | lib/std/meta.zig | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/std/meta.zig b/lib/std/meta.zig index 58fd6b9da7..65809abb5c 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -115,6 +115,32 @@ test "std.meta.Child" { testing.expect(Child(?u8) == u8); } +/// Given a type with a sentinel e.g. `[:0]u8`, returns the sentinel +pub fn Sentinel(comptime T: type) Child(T) { + // comptime asserts that ptr has a sentinel + switch (@typeInfo(T)) { + .Array => |arrayInfo| { + return comptime arrayInfo.sentinel.?; + }, + .Pointer => |ptrInfo| { + switch (ptrInfo.size) { + .Many, .Slice => { + return comptime ptrInfo.sentinel.?; + }, + else => {}, + } + }, + else => {}, + } + @compileError("not a sentinel type, found '" ++ @typeName(T) ++ "'"); +} + +test "std.meta.Sentinel" { + testing.expectEqual(@as(u8, 0), Sentinel([:0]u8)); + testing.expectEqual(@as(u8, 0), Sentinel([*:0]u8)); + testing.expectEqual(@as(u8, 0), Sentinel([5:0]u8)); +} + pub fn containerLayout(comptime T: type) TypeInfo.ContainerLayout { return switch (@typeInfo(T)) { .Struct => |info| info.layout, |
