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
signature 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 {...@@ -104,7 +104,7 @@ pub fn Child(comptime T: type) type {
104 .Array => |info| info.child,104 .Array => |info| info.child,
105 .Pointer => |info| info.child,105 .Pointer => |info| info.child,
106 .Optional => |info| info.child,106 .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) ++ "'"),
108 };108 };
109}109}
110110
...@@ -115,16 +115,39 @@ test "std.meta.Child" {...@@ -115,16 +115,39 @@ test "std.meta.Child" {
115 testing.expect(Child(?u8) == u8);115 testing.expect(Child(?u8) == u8);
116}116}
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
118/// Given a type which can have a sentinel e.g. `[:0]u8`, returns the sentinel value,141/// Given a type which can have a sentinel e.g. `[:0]u8`, returns the sentinel value,
119/// or `null` if there is not one.142/// or `null` if there is not one.
120/// Types which cannot possibly have a sentinel will be a compile error.143/// 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) {
122 switch (@typeInfo(T)) {145 switch (@typeInfo(T)) {
123 .Array => |info| return info.sentinel,146 .Array => |info| return info.sentinel,
124 .Pointer => |info| {147 .Pointer => |info| {
125 switch (info.size) {148 switch (info.size) {
126 .Many, .Slice => return info.sentinel,149 .Many, .Slice => return info.sentinel,
127 .One => switch (info.child) {150 .One => switch (@typeInfo(info.child)) {
128 .Array => |array_info| return array_info.sentinel,151 .Array => |array_info| return array_info.sentinel,
129 else => {},152 else => {},
130 },153 },
...@@ -137,6 +160,11 @@ pub fn sentinel(comptime T: type) ?Child(T) {...@@ -137,6 +160,11 @@ pub fn sentinel(comptime T: type) ?Child(T) {
137}160}
138161
139test "std.meta.sentinel" {162test "std.meta.sentinel" {
163 testSentinel();
164 comptime testSentinel();
165}
166
167fn testSentinel() void {
140 testing.expectEqual(@as(u8, 0), sentinel([:0]u8).?);168 testing.expectEqual(@as(u8, 0), sentinel([:0]u8).?);
141 testing.expectEqual(@as(u8, 0), sentinel([*:0]u8).?);169 testing.expectEqual(@as(u8, 0), sentinel([*:0]u8).?);
142 testing.expectEqual(@as(u8, 0), sentinel([5:0]u8).?);170 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" {...@@ -159,12 +159,13 @@ test "allowzero pointer and slice" {
159 var opt_ptr: ?[*]allowzero i32 = ptr;159 var opt_ptr: ?[*]allowzero i32 = ptr;
160 expect(opt_ptr != null);160 expect(opt_ptr != null);
161 expect(@ptrToInt(ptr) == 0);161 expect(@ptrToInt(ptr) == 0);
162 var slice = ptr[0..10];162 var runtime_zero: usize = 0;
163 expect(@TypeOf(slice) == []allowzero i32);163 var slice = ptr[runtime_zero..10];
164 comptime expect(@TypeOf(slice) == []allowzero i32);
164 expect(@ptrToInt(&slice[5]) == 20);165 expect(@ptrToInt(&slice[5]) == 20);
165166
166 expect(@typeInfo(@TypeOf(ptr)).Pointer.is_allowzero);167 comptime expect(@typeInfo(@TypeOf(ptr)).Pointer.is_allowzero);
167 expect(@typeInfo(@TypeOf(slice)).Pointer.is_allowzero);168 comptime expect(@typeInfo(@TypeOf(slice)).Pointer.is_allowzero);
168}169}
169170
170test "assign null directly to C pointer and test null equality" {171test "assign null directly to C pointer and test null equality" {