Skip to content

Commit 141c548

Browse files
committed
fixes to std.meta
behavior tests are passing now
1 parent 371d36a commit 141c548

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

lib/std/meta.zig

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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".
119+
pub 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+
134+
test "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.
121-
pub fn sentinel(comptime T: type) ?Child(T) {
144+
pub 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

139162
test "std.meta.sentinel" {
163+
testSentinel();
164+
comptime testSentinel();
165+
}
166+
167+
fn 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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

170171
test "assign null directly to C pointer and test null equality" {

0 commit comments

Comments
 (0)