authorgravatar for 14938807+xackus@users.noreply.github.comxackus <14938807+xackus@users.noreply.github.com> 2020-02-13 23:20:40+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-14 09:35:38-05:00
log7396b144ba72d28d10ee1e089fc8e4f87239a840
tree7b15efc5ba88641c730cffbef8b6540b6d986e30
parentfb6b94f80fed42b4c690b43a875bf0a58977493e

modernize std.meta


2 files changed, 95 insertions(+), 126 deletions(-)

lib/std/meta.zig+52-62
...@@ -7,13 +7,12 @@ const testing = std.testing;...@@ -7,13 +7,12 @@ const testing = std.testing;
77
8pub const trait = @import("meta/trait.zig");8pub const trait = @import("meta/trait.zig");
99
10const TypeId = builtin.TypeId;
11const TypeInfo = builtin.TypeInfo;10const TypeInfo = builtin.TypeInfo;
1211
13pub fn tagName(v: var) []const u8 {12pub fn tagName(v: var) []const u8 {
14 const T = @TypeOf(v);13 const T = @TypeOf(v);
15 switch (@typeInfo(T)) {14 switch (@typeInfo(T)) {
16 TypeId.ErrorSet => return @errorName(v),15 .ErrorSet => return @errorName(v),
17 else => return @tagName(v),16 else => return @tagName(v),
18 }17 }
19}18}
...@@ -55,7 +54,7 @@ test "std.meta.tagName" {...@@ -55,7 +54,7 @@ test "std.meta.tagName" {
5554
56pub fn stringToEnum(comptime T: type, str: []const u8) ?T {55pub fn stringToEnum(comptime T: type, str: []const u8) ?T {
57 inline for (@typeInfo(T).Enum.fields) |enumField| {56 inline for (@typeInfo(T).Enum.fields) |enumField| {
58 if (std.mem.eql(u8, str, enumField.name)) {57 if (mem.eql(u8, str, enumField.name)) {
59 return @field(T, enumField.name);58 return @field(T, enumField.name);
60 }59 }
61 }60 }
...@@ -74,9 +73,9 @@ test "std.meta.stringToEnum" {...@@ -74,9 +73,9 @@ test "std.meta.stringToEnum" {
7473
75pub fn bitCount(comptime T: type) comptime_int {74pub fn bitCount(comptime T: type) comptime_int {
76 return switch (@typeInfo(T)) {75 return switch (@typeInfo(T)) {
77 TypeId.Bool => 1,76 .Bool => 1,
78 TypeId.Int => |info| info.bits,77 .Int => |info| info.bits,
79 TypeId.Float => |info| info.bits,78 .Float => |info| info.bits,
80 else => @compileError("Expected bool, int or float type, found '" ++ @typeName(T) ++ "'"),79 else => @compileError("Expected bool, int or float type, found '" ++ @typeName(T) ++ "'"),
81 };80 };
82}81}
...@@ -88,7 +87,7 @@ test "std.meta.bitCount" {...@@ -88,7 +87,7 @@ test "std.meta.bitCount" {
8887
89pub fn alignment(comptime T: type) comptime_int {88pub fn alignment(comptime T: type) comptime_int {
90 //@alignOf works on non-pointer types89 //@alignOf works on non-pointer types
91 const P = if (comptime trait.is(TypeId.Pointer)(T)) T else *T;90 const P = if (comptime trait.is(.Pointer)(T)) T else *T;
92 return @typeInfo(P).Pointer.alignment;91 return @typeInfo(P).Pointer.alignment;
93}92}
9493
...@@ -102,9 +101,9 @@ test "std.meta.alignment" {...@@ -102,9 +101,9 @@ test "std.meta.alignment" {
102101
103pub fn Child(comptime T: type) type {102pub fn Child(comptime T: type) type {
104 return switch (@typeInfo(T)) {103 return switch (@typeInfo(T)) {
105 TypeId.Array => |info| info.child,104 .Array => |info| info.child,
106 TypeId.Pointer => |info| info.child,105 .Pointer => |info| info.child,
107 TypeId.Optional => |info| info.child,106 .Optional => |info| info.child,
108 else => @compileError("Expected pointer, optional, or array type, " ++ "found '" ++ @typeName(T) ++ "'"),107 else => @compileError("Expected pointer, optional, or array type, " ++ "found '" ++ @typeName(T) ++ "'"),
109 };108 };
110}109}
...@@ -118,9 +117,9 @@ test "std.meta.Child" {...@@ -118,9 +117,9 @@ test "std.meta.Child" {
118117
119pub fn containerLayout(comptime T: type) TypeInfo.ContainerLayout {118pub fn containerLayout(comptime T: type) TypeInfo.ContainerLayout {
120 return switch (@typeInfo(T)) {119 return switch (@typeInfo(T)) {
121 TypeId.Struct => |info| info.layout,120 .Struct => |info| info.layout,
122 TypeId.Enum => |info| info.layout,121 .Enum => |info| info.layout,
123 TypeId.Union => |info| info.layout,122 .Union => |info| info.layout,
124 else => @compileError("Expected struct, enum or union type, found '" ++ @typeName(T) ++ "'"),123 else => @compileError("Expected struct, enum or union type, found '" ++ @typeName(T) ++ "'"),
125 };124 };
126}125}
...@@ -148,22 +147,22 @@ test "std.meta.containerLayout" {...@@ -148,22 +147,22 @@ test "std.meta.containerLayout" {
148 a: u8,147 a: u8,
149 };148 };
150149
151 testing.expect(containerLayout(E1) == TypeInfo.ContainerLayout.Auto);150 testing.expect(containerLayout(E1) == .Auto);
152 testing.expect(containerLayout(E2) == TypeInfo.ContainerLayout.Packed);151 testing.expect(containerLayout(E2) == .Packed);
153 testing.expect(containerLayout(E3) == TypeInfo.ContainerLayout.Extern);152 testing.expect(containerLayout(E3) == .Extern);
154 testing.expect(containerLayout(S1) == TypeInfo.ContainerLayout.Auto);153 testing.expect(containerLayout(S1) == .Auto);
155 testing.expect(containerLayout(S2) == TypeInfo.ContainerLayout.Packed);154 testing.expect(containerLayout(S2) == .Packed);
156 testing.expect(containerLayout(S3) == TypeInfo.ContainerLayout.Extern);155 testing.expect(containerLayout(S3) == .Extern);
157 testing.expect(containerLayout(U1) == TypeInfo.ContainerLayout.Auto);156 testing.expect(containerLayout(U1) == .Auto);
158 testing.expect(containerLayout(U2) == TypeInfo.ContainerLayout.Packed);157 testing.expect(containerLayout(U2) == .Packed);
159 testing.expect(containerLayout(U3) == TypeInfo.ContainerLayout.Extern);158 testing.expect(containerLayout(U3) == .Extern);
160}159}
161160
162pub fn declarations(comptime T: type) []TypeInfo.Declaration {161pub fn declarations(comptime T: type) []TypeInfo.Declaration {
163 return switch (@typeInfo(T)) {162 return switch (@typeInfo(T)) {
164 TypeId.Struct => |info| info.decls,163 .Struct => |info| info.decls,
165 TypeId.Enum => |info| info.decls,164 .Enum => |info| info.decls,
166 TypeId.Union => |info| info.decls,165 .Union => |info| info.decls,
167 else => @compileError("Expected struct, enum or union type, found '" ++ @typeName(T) ++ "'"),166 else => @compileError("Expected struct, enum or union type, found '" ++ @typeName(T) ++ "'"),
168 };167 };
169}168}
...@@ -232,17 +231,17 @@ test "std.meta.declarationInfo" {...@@ -232,17 +231,17 @@ test "std.meta.declarationInfo" {
232}231}
233232
234pub fn fields(comptime T: type) switch (@typeInfo(T)) {233pub fn fields(comptime T: type) switch (@typeInfo(T)) {
235 TypeId.Struct => []TypeInfo.StructField,234 .Struct => []TypeInfo.StructField,
236 TypeId.Union => []TypeInfo.UnionField,235 .Union => []TypeInfo.UnionField,
237 TypeId.ErrorSet => []TypeInfo.Error,236 .ErrorSet => []TypeInfo.Error,
238 TypeId.Enum => []TypeInfo.EnumField,237 .Enum => []TypeInfo.EnumField,
239 else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"),238 else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"),
240} {239} {
241 return switch (@typeInfo(T)) {240 return switch (@typeInfo(T)) {
242 TypeId.Struct => |info| info.fields,241 .Struct => |info| info.fields,
243 TypeId.Union => |info| info.fields,242 .Union => |info| info.fields,
244 TypeId.Enum => |info| info.fields,243 .Enum => |info| info.fields,
245 TypeId.ErrorSet => |errors| errors.?, // must be non global error set244 .ErrorSet => |errors| errors.?, // must be non global error set
246 else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"),245 else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"),
247 };246 };
248}247}
...@@ -277,10 +276,10 @@ test "std.meta.fields" {...@@ -277,10 +276,10 @@ test "std.meta.fields" {
277}276}
278277
279pub fn fieldInfo(comptime T: type, comptime field_name: []const u8) switch (@typeInfo(T)) {278pub fn fieldInfo(comptime T: type, comptime field_name: []const u8) switch (@typeInfo(T)) {
280 TypeId.Struct => TypeInfo.StructField,279 .Struct => TypeInfo.StructField,
281 TypeId.Union => TypeInfo.UnionField,280 .Union => TypeInfo.UnionField,
282 TypeId.ErrorSet => TypeInfo.Error,281 .ErrorSet => TypeInfo.Error,
283 TypeId.Enum => TypeInfo.EnumField,282 .Enum => TypeInfo.EnumField,
284 else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"),283 else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"),
285} {284} {
286 inline for (comptime fields(T)) |field| {285 inline for (comptime fields(T)) |field| {
...@@ -318,8 +317,8 @@ test "std.meta.fieldInfo" {...@@ -318,8 +317,8 @@ test "std.meta.fieldInfo" {
318317
319pub fn TagType(comptime T: type) type {318pub fn TagType(comptime T: type) type {
320 return switch (@typeInfo(T)) {319 return switch (@typeInfo(T)) {
321 TypeId.Enum => |info| info.tag_type,320 .Enum => |info| info.tag_type,
322 TypeId.Union => |info| if (info.tag_type) |Tag| Tag else null,321 .Union => |info| if (info.tag_type) |Tag| Tag else null,
323 else => @compileError("expected enum or union type, found '" ++ @typeName(T) ++ "'"),322 else => @compileError("expected enum or union type, found '" ++ @typeName(T) ++ "'"),
324 };323 };
325}324}
...@@ -365,7 +364,7 @@ test "std.meta.activeTag" {...@@ -365,7 +364,7 @@ test "std.meta.activeTag" {
365///Given a tagged union type, and an enum, return the type of the union364///Given a tagged union type, and an enum, return the type of the union
366/// field corresponding to the enum tag.365/// field corresponding to the enum tag.
367pub fn TagPayloadType(comptime U: type, tag: @TagType(U)) type {366pub fn TagPayloadType(comptime U: type, tag: @TagType(U)) type {
368 testing.expect(trait.is(builtin.TypeId.Union)(U));367 testing.expect(trait.is(.Union)(U));
369368
370 const info = @typeInfo(U).Union;369 const info = @typeInfo(U).Union;
371370
...@@ -387,30 +386,26 @@ test "std.meta.TagPayloadType" {...@@ -387,30 +386,26 @@ test "std.meta.TagPayloadType" {
387 testing.expect(MovedEvent == @TypeOf(e.Moved));386 testing.expect(MovedEvent == @TypeOf(e.Moved));
388}387}
389388
390///Compares two of any type for equality. Containers are compared on a field-by-field basis,389/// Compares two of any type for equality. Containers are compared on a field-by-field basis,
391/// where possible. Pointers are not followed.390/// where possible. Pointers are not followed.
392pub fn eql(a: var, b: @TypeOf(a)) bool {391pub fn eql(a: var, b: @TypeOf(a)) bool {
393 const T = @TypeOf(a);392 const T = @TypeOf(a);
394393
395 switch (@typeId(T)) {394 switch (@typeInfo(T)) {
396 builtin.TypeId.Struct => {395 .Struct => |info| {
397 const info = @typeInfo(T).Struct;
398
399 inline for (info.fields) |field_info| {396 inline for (info.fields) |field_info| {
400 if (!eql(@field(a, field_info.name), @field(b, field_info.name))) return false;397 if (!eql(@field(a, field_info.name), @field(b, field_info.name))) return false;
401 }398 }
402 return true;399 return true;
403 },400 },
404 builtin.TypeId.ErrorUnion => {401 .ErrorUnion => {
405 if (a) |a_p| {402 if (a) |a_p| {
406 if (b) |b_p| return eql(a_p, b_p) else |_| return false;403 if (b) |b_p| return eql(a_p, b_p) else |_| return false;
407 } else |a_e| {404 } else |a_e| {
408 if (b) |_| return false else |b_e| return a_e == b_e;405 if (b) |_| return false else |b_e| return a_e == b_e;
409 }406 }
410 },407 },
411 builtin.TypeId.Union => {408 .Union => |info| {
412 const info = @typeInfo(T).Union;
413
414 if (info.tag_type) |_| {409 if (info.tag_type) |_| {
415 const tag_a = activeTag(a);410 const tag_a = activeTag(a);
416 const tag_b = activeTag(b);411 const tag_b = activeTag(b);
...@@ -427,31 +422,26 @@ pub fn eql(a: var, b: @TypeOf(a)) bool {...@@ -427,31 +422,26 @@ pub fn eql(a: var, b: @TypeOf(a)) bool {
427422
428 @compileError("cannot compare untagged union type " ++ @typeName(T));423 @compileError("cannot compare untagged union type " ++ @typeName(T));
429 },424 },
430 builtin.TypeId.Array => {425 .Array => {
431 if (a.len != b.len) return false;426 if (a.len != b.len) return false;
432 for (a) |e, i|427 for (a) |e, i|
433 if (!eql(e, b[i])) return false;428 if (!eql(e, b[i])) return false;
434 return true;429 return true;
435 },430 },
436 builtin.TypeId.Vector => {431 .Vector => |info| {
437 const info = @typeInfo(T).Vector;
438 var i: usize = 0;432 var i: usize = 0;
439 while (i < info.len) : (i += 1) {433 while (i < info.len) : (i += 1) {
440 if (!eql(a[i], b[i])) return false;434 if (!eql(a[i], b[i])) return false;
441 }435 }
442 return true;436 return true;
443 },437 },
444 builtin.TypeId.Pointer => {438 .Pointer => |info| {
445 const info = @typeInfo(T).Pointer;439 return switch (info.size) {
446 switch (info.size) {440 .One, .Many, .C, => a == b,
447 builtin.TypeInfo.Pointer.Size.One,441 .Slice => a.ptr == b.ptr and a.len == b.len,
448 builtin.TypeInfo.Pointer.Size.Many,442 };
449 builtin.TypeInfo.Pointer.Size.C,
450 => return a == b,
451 builtin.TypeInfo.Pointer.Size.Slice => return a.ptr == b.ptr and a.len == b.len,
452 }
453 },443 },
454 builtin.TypeId.Optional => {444 .Optional => {
455 if (a == null and b == null) return true;445 if (a == null and b == null) return true;
456 if (a == null or b == null) return false;446 if (a == null or b == null) return false;
457 return eql(a.?, b.?);447 return eql(a.?, b.?);
lib/std/meta/trait.zig+43-64
...@@ -7,17 +7,11 @@ const warn = debug.warn;...@@ -7,17 +7,11 @@ const warn = debug.warn;
77
8const meta = @import("../meta.zig");8const meta = @import("../meta.zig");
99
10//This is necessary if we want to return generic functions directly because of how the10pub const TraitFn = fn (type) bool;
11// the type erasure works. see: #1375
12fn traitFnWorkaround(comptime T: type) bool {
13 return false;
14}
15
16pub const TraitFn = @TypeOf(traitFnWorkaround);
1711
18//////Trait generators12//////Trait generators
1913
20//Need TraitList because compiler can't do varargs at comptime yet14// TODO convert to tuples when #4335 is done
21pub const TraitList = []const TraitFn;15pub const TraitList = []const TraitFn;
22pub fn multiTrait(comptime traits: TraitList) TraitFn {16pub fn multiTrait(comptime traits: TraitList) TraitFn {
23 const Closure = struct {17 const Closure = struct {
...@@ -60,8 +54,7 @@ pub fn hasFn(comptime name: []const u8) TraitFn {...@@ -60,8 +54,7 @@ pub fn hasFn(comptime name: []const u8) TraitFn {
60 if (!comptime isContainer(T)) return false;54 if (!comptime isContainer(T)) return false;
61 if (!comptime @hasDecl(T, name)) return false;55 if (!comptime @hasDecl(T, name)) return false;
62 const DeclType = @TypeOf(@field(T, name));56 const DeclType = @TypeOf(@field(T, name));
63 const decl_type_id = @typeId(DeclType);57 return @typeId(DeclType) == .Fn;
64 return decl_type_id == builtin.TypeId.Fn;
65 }58 }
66 };59 };
67 return Closure.trait;60 return Closure.trait;
...@@ -80,11 +73,10 @@ test "std.meta.trait.hasFn" {...@@ -80,11 +73,10 @@ test "std.meta.trait.hasFn" {
80pub fn hasField(comptime name: []const u8) TraitFn {73pub fn hasField(comptime name: []const u8) TraitFn {
81 const Closure = struct {74 const Closure = struct {
82 pub fn trait(comptime T: type) bool {75 pub fn trait(comptime T: type) bool {
83 const info = @typeInfo(T);76 const fields = switch (@typeInfo(T)) {
84 const fields = switch (info) {77 .Struct => |s| s.fields,
85 builtin.TypeId.Struct => |s| s.fields,78 .Union => |u| u.fields,
86 builtin.TypeId.Union => |u| u.fields,79 .Enum => |e| e.fields,
87 builtin.TypeId.Enum => |e| e.fields,
88 else => return false,80 else => return false,
89 };81 };
9082
...@@ -120,11 +112,11 @@ pub fn is(comptime id: builtin.TypeId) TraitFn {...@@ -120,11 +112,11 @@ pub fn is(comptime id: builtin.TypeId) TraitFn {
120}112}
121113
122test "std.meta.trait.is" {114test "std.meta.trait.is" {
123 testing.expect(is(builtin.TypeId.Int)(u8));115 testing.expect(is(.Int)(u8));
124 testing.expect(!is(builtin.TypeId.Int)(f32));116 testing.expect(!is(.Int)(f32));
125 testing.expect(is(builtin.TypeId.Pointer)(*u8));117 testing.expect(is(.Pointer)(*u8));
126 testing.expect(is(builtin.TypeId.Void)(void));118 testing.expect(is(.Void)(void));
127 testing.expect(!is(builtin.TypeId.Optional)(anyerror));119 testing.expect(!is(.Optional)(anyerror));
128}120}
129121
130pub fn isPtrTo(comptime id: builtin.TypeId) TraitFn {122pub fn isPtrTo(comptime id: builtin.TypeId) TraitFn {
...@@ -138,9 +130,9 @@ pub fn isPtrTo(comptime id: builtin.TypeId) TraitFn {...@@ -138,9 +130,9 @@ pub fn isPtrTo(comptime id: builtin.TypeId) TraitFn {
138}130}
139131
140test "std.meta.trait.isPtrTo" {132test "std.meta.trait.isPtrTo" {
141 testing.expect(!isPtrTo(builtin.TypeId.Struct)(struct {}));133 testing.expect(!isPtrTo(.Struct)(struct {}));
142 testing.expect(isPtrTo(builtin.TypeId.Struct)(*struct {}));134 testing.expect(isPtrTo(.Struct)(*struct {}));
143 testing.expect(!isPtrTo(builtin.TypeId.Struct)(**struct {}));135 testing.expect(!isPtrTo(.Struct)(**struct {}));
144}136}
145137
146///////////Strait trait Fns138///////////Strait trait Fns
...@@ -149,12 +141,10 @@ test "std.meta.trait.isPtrTo" {...@@ -149,12 +141,10 @@ test "std.meta.trait.isPtrTo" {
149// Somewhat limited since we can't apply this logic to normal variables, fields, or141// Somewhat limited since we can't apply this logic to normal variables, fields, or
150// Fns yet. Should be isExternType?142// Fns yet. Should be isExternType?
151pub fn isExtern(comptime T: type) bool {143pub fn isExtern(comptime T: type) bool {
152 const Extern = builtin.TypeInfo.ContainerLayout.Extern;144 return switch (@typeInfo(T)) {
153 const info = @typeInfo(T);145 .Struct => |s| s.layout == .Extern,
154 return switch (info) {146 .Union => |u| u.layout == .Extern,
155 builtin.TypeId.Struct => |s| s.layout == Extern,147 .Enum => |e| e.layout == .Extern,
156 builtin.TypeId.Union => |u| u.layout == Extern,
157 builtin.TypeId.Enum => |e| e.layout == Extern,
158 else => false,148 else => false,
159 };149 };
160}150}
...@@ -169,12 +159,10 @@ test "std.meta.trait.isExtern" {...@@ -169,12 +159,10 @@ test "std.meta.trait.isExtern" {
169}159}
170160
171pub fn isPacked(comptime T: type) bool {161pub fn isPacked(comptime T: type) bool {
172 const Packed = builtin.TypeInfo.ContainerLayout.Packed;162 return switch (@typeInfo(T)) {
173 const info = @typeInfo(T);163 .Struct => |s| s.layout == .Packed,
174 return switch (info) {164 .Union => |u| u.layout == .Packed,
175 builtin.TypeId.Struct => |s| s.layout == Packed,165 .Enum => |e| e.layout == .Packed,
176 builtin.TypeId.Union => |u| u.layout == Packed,
177 builtin.TypeId.Enum => |e| e.layout == Packed,
178 else => false,166 else => false,
179 };167 };
180}168}
...@@ -189,8 +177,8 @@ test "std.meta.trait.isPacked" {...@@ -189,8 +177,8 @@ test "std.meta.trait.isPacked" {
189}177}
190178
191pub fn isUnsignedInt(comptime T: type) bool {179pub fn isUnsignedInt(comptime T: type) bool {
192 return switch (@typeId(T)) {180 return switch (@typeInfo(T)) {
193 builtin.TypeId.Int => !@typeInfo(T).Int.is_signed,181 .Int => |i| !i.is_signed,
194 else => false,182 else => false,
195 };183 };
196}184}
...@@ -203,9 +191,9 @@ test "isUnsignedInt" {...@@ -203,9 +191,9 @@ test "isUnsignedInt" {
203}191}
204192
205pub fn isSignedInt(comptime T: type) bool {193pub fn isSignedInt(comptime T: type) bool {
206 return switch (@typeId(T)) {194 return switch (@typeInfo(T)) {
207 builtin.TypeId.ComptimeInt => true,195 .ComptimeInt => true,
208 builtin.TypeId.Int => @typeInfo(T).Int.is_signed,196 .Int => |i| i.is_signed,
209 else => false,197 else => false,
210 };198 };
211}199}
...@@ -218,9 +206,8 @@ test "isSignedInt" {...@@ -218,9 +206,8 @@ test "isSignedInt" {
218}206}
219207
220pub fn isSingleItemPtr(comptime T: type) bool {208pub fn isSingleItemPtr(comptime T: type) bool {
221 if (comptime is(builtin.TypeId.Pointer)(T)) {209 if (comptime is(.Pointer)(T)) {
222 const info = @typeInfo(T);210 return @typeInfo(T).Pointer.size == .One;
223 return info.Pointer.size == builtin.TypeInfo.Pointer.Size.One;
224 }211 }
225 return false;212 return false;
226}213}
...@@ -233,9 +220,8 @@ test "std.meta.trait.isSingleItemPtr" {...@@ -233,9 +220,8 @@ test "std.meta.trait.isSingleItemPtr" {
233}220}
234221
235pub fn isManyItemPtr(comptime T: type) bool {222pub fn isManyItemPtr(comptime T: type) bool {
236 if (comptime is(builtin.TypeId.Pointer)(T)) {223 if (comptime is(.Pointer)(T)) {
237 const info = @typeInfo(T);224 return @typeInfo(T).Pointer.size == .Many;
238 return info.Pointer.size == builtin.TypeInfo.Pointer.Size.Many;
239 }225 }
240 return false;226 return false;
241}227}
...@@ -249,9 +235,8 @@ test "std.meta.trait.isManyItemPtr" {...@@ -249,9 +235,8 @@ test "std.meta.trait.isManyItemPtr" {
249}235}
250236
251pub fn isSlice(comptime T: type) bool {237pub fn isSlice(comptime T: type) bool {
252 if (comptime is(builtin.TypeId.Pointer)(T)) {238 if (comptime is(.Pointer)(T)) {
253 const info = @typeInfo(T);239 return @typeInfo(T).Pointer.size == .Slice;
254 return info.Pointer.size == builtin.TypeInfo.Pointer.Size.Slice;
255 }240 }
256 return false;241 return false;
257}242}
...@@ -264,15 +249,13 @@ test "std.meta.trait.isSlice" {...@@ -264,15 +249,13 @@ test "std.meta.trait.isSlice" {
264}249}
265250
266pub fn isIndexable(comptime T: type) bool {251pub fn isIndexable(comptime T: type) bool {
267 if (comptime is(builtin.TypeId.Pointer)(T)) {252 if (comptime is(.Pointer)(T)) {
268 const info = @typeInfo(T);253 if (@typeInfo(T).Pointer.size == .One) {
269 if (info.Pointer.size == builtin.TypeInfo.Pointer.Size.One) {254 return (comptime is(.Array)(meta.Child(T)));
270 if (comptime is(builtin.TypeId.Array)(meta.Child(T))) return true;
271 return false;
272 }255 }
273 return true;256 return true;
274 }257 }
275 return comptime is(builtin.TypeId.Array)(T);258 return comptime is(.Array)(T);
276}259}
277260
278test "std.meta.trait.isIndexable" {261test "std.meta.trait.isIndexable" {
...@@ -287,7 +270,7 @@ test "std.meta.trait.isIndexable" {...@@ -287,7 +270,7 @@ test "std.meta.trait.isIndexable" {
287270
288pub fn isNumber(comptime T: type) bool {271pub fn isNumber(comptime T: type) bool {
289 return switch (@typeId(T)) {272 return switch (@typeId(T)) {
290 builtin.TypeId.Int, builtin.TypeId.Float, builtin.TypeId.ComptimeInt, builtin.TypeId.ComptimeFloat => true,273 .Int, .Float, .ComptimeInt, .ComptimeFloat => true,
291 else => false,274 else => false,
292 };275 };
293}276}
...@@ -307,9 +290,8 @@ test "std.meta.trait.isNumber" {...@@ -307,9 +290,8 @@ test "std.meta.trait.isNumber" {
307}290}
308291
309pub fn isConstPtr(comptime T: type) bool {292pub fn isConstPtr(comptime T: type) bool {
310 if (!comptime is(builtin.TypeId.Pointer)(T)) return false;293 if (!comptime is(.Pointer)(T)) return false;
311 const info = @typeInfo(T);294 return @typeInfo(T).Pointer.is_const;
312 return info.Pointer.is_const;
313}295}
314296
315test "std.meta.trait.isConstPtr" {297test "std.meta.trait.isConstPtr" {
...@@ -322,11 +304,8 @@ test "std.meta.trait.isConstPtr" {...@@ -322,11 +304,8 @@ test "std.meta.trait.isConstPtr" {
322}304}
323305
324pub fn isContainer(comptime T: type) bool {306pub fn isContainer(comptime T: type) bool {
325 const info = @typeInfo(T);307 return switch (@typeId(T)) {
326 return switch (info) {308 .Struct, .Union, .Enum => true,
327 builtin.TypeId.Struct => true,
328 builtin.TypeId.Union => true,
329 builtin.TypeId.Enum => true,
330 else => false,309 else => false,
331 };310 };
332}311}