authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-18 19:29:24-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-19 09:53:55-04:00
logb5dba702fff35c2d9aa86c9d5dd93a4a38d3b75b
treebb25768327c4170f7a88246942b097594c8229cc
parent2164b511cce235ec4a49de99452e4900835bfba8
signaturelock-open Commit is signed but in an unrecognized format.

fixes to std.meta

behavior tests are passing now

2 files changed, 36 insertions(+), 7 deletions(-)

lib/std/meta.zig+31-3
......@@ -104,7 +104,7 @@ pub fn Child(comptime T: type) type {
104104 .Array => |info| info.child,
105105 .Pointer => |info| info.child,
106106 .Optional => |info| info.child,
107 else => @compileError("Expected pointer, optional, or array type, " ++ "found '" ++ @typeName(T) ++ "'"),
107 else => @compileError("Expected pointer, optional, or array type, found '" ++ @typeName(T) ++ "'"),
108108 };
109109}
110110
......@@ -115,16 +115,39 @@ test "std.meta.Child" {
115115 testing.expect(Child(?u8) == u8);
116116}
117117
118/// Given a "memory span" type, returns the "element type".
119pub fn Elem(comptime T: type) type {
120 switch (@typeInfo(T)) {
121 .Array => |info| return info.child,
122 .Pointer => |info| switch (info.size) {
123 .One => switch (@typeInfo(info.child)) {
124 .Array => |array_info| return array_info.child,
125 else => {},
126 },
127 .Many, .C, .Slice => return info.child,
128 },
129 else => {},
130 }
131 @compileError("Expected pointer, slice, or array, found '" ++ @typeName(T) ++ "'");
132}
133
134test "std.meta.Elem" {
135 testing.expect(Elem([1]u8) == u8);
136 testing.expect(Elem([*]u8) == u8);
137 testing.expect(Elem([]u8) == u8);
138 testing.expect(Elem(*[10]u8) == u8);
139}
140
118141/// Given a type which can have a sentinel e.g. `[:0]u8`, returns the sentinel value,
119142/// or `null` if there is not one.
120143/// Types which cannot possibly have a sentinel will be a compile error.
121pub fn sentinel(comptime T: type) ?Child(T) {
144pub fn sentinel(comptime T: type) ?Elem(T) {
122145 switch (@typeInfo(T)) {
123146 .Array => |info| return info.sentinel,
124147 .Pointer => |info| {
125148 switch (info.size) {
126149 .Many, .Slice => return info.sentinel,
127 .One => switch (info.child) {
150 .One => switch (@typeInfo(info.child)) {
128151 .Array => |array_info| return array_info.sentinel,
129152 else => {},
130153 },
......@@ -137,6 +160,11 @@ pub fn sentinel(comptime T: type) ?Child(T) {
137160}
138161
139162test "std.meta.sentinel" {
163 testSentinel();
164 comptime testSentinel();
165}
166
167fn testSentinel() void {
140168 testing.expectEqual(@as(u8, 0), sentinel([:0]u8).?);
141169 testing.expectEqual(@as(u8, 0), sentinel([*:0]u8).?);
142170 testing.expectEqual(@as(u8, 0), sentinel([5:0]u8).?);
test/stage1/behavior/pointers.zig+5-4
......@@ -159,12 +159,13 @@ test "allowzero pointer and slice" {
159159 var opt_ptr: ?[*]allowzero i32 = ptr;
160160 expect(opt_ptr != null);
161161 expect(@ptrToInt(ptr) == 0);
162 var slice = ptr[0..10];
163 expect(@TypeOf(slice) == []allowzero i32);
162 var runtime_zero: usize = 0;
163 var slice = ptr[runtime_zero..10];
164 comptime expect(@TypeOf(slice) == []allowzero i32);
164165 expect(@ptrToInt(&slice[5]) == 20);
165166
166 expect(@typeInfo(@TypeOf(ptr)).Pointer.is_allowzero);
167 expect(@typeInfo(@TypeOf(slice)).Pointer.is_allowzero);
167 comptime expect(@typeInfo(@TypeOf(ptr)).Pointer.is_allowzero);
168 comptime expect(@typeInfo(@TypeOf(slice)).Pointer.is_allowzero);
168169}
169170
170171test "assign null directly to C pointer and test null equality" {