authorgravatar for datamanrb@gmail.comdata-man <datamanrb@gmail.com> 2020-05-26 15:52:37+05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-26 10:56:29-04:00
logd10e407977f1ba165ca3d0a04d7b270096185559
treea89b8c94075357e3c657728a6649efa81d6ca4f0
parent4b8077ea8e888382fc73dd8a19c6e9160f2cef46

More vector support in std.meta


1 files changed, 9 insertions(+), 3 deletions(-)

lib/std/meta.zig+9-3
...@@ -102,9 +102,10 @@ test "std.meta.alignment" {...@@ -102,9 +102,10 @@ test "std.meta.alignment" {
102pub fn Child(comptime T: type) type {102pub fn Child(comptime T: type) type {
103 return switch (@typeInfo(T)) {103 return switch (@typeInfo(T)) {
104 .Array => |info| info.child,104 .Array => |info| info.child,
105 .Vector => |info| info.child,
105 .Pointer => |info| info.child,106 .Pointer => |info| info.child,
106 .Optional => |info| info.child,107 .Optional => |info| info.child,
107 else => @compileError("Expected pointer, optional, or array type, found '" ++ @typeName(T) ++ "'"),108 else => @compileError("Expected pointer, optional, array or vector type, found '" ++ @typeName(T) ++ "'"),
108 };109 };
109}110}
110111
...@@ -113,22 +114,25 @@ test "std.meta.Child" {...@@ -113,22 +114,25 @@ test "std.meta.Child" {
113 testing.expect(Child(*u8) == u8);114 testing.expect(Child(*u8) == u8);
114 testing.expect(Child([]u8) == u8);115 testing.expect(Child([]u8) == u8);
115 testing.expect(Child(?u8) == u8);116 testing.expect(Child(?u8) == u8);
117 testing.expect(Child(Vector(2, u8)) == u8);
116}118}
117119
118/// Given a "memory span" type, returns the "element type".120/// Given a "memory span" type, returns the "element type".
119pub fn Elem(comptime T: type) type {121pub fn Elem(comptime T: type) type {
120 switch (@typeInfo(T)) {122 switch (@typeInfo(T)) {
121 .Array => |info| return info.child,123 .Array, => |info| return info.child,
124 .Vector, => |info| return info.child,
122 .Pointer => |info| switch (info.size) {125 .Pointer => |info| switch (info.size) {
123 .One => switch (@typeInfo(info.child)) {126 .One => switch (@typeInfo(info.child)) {
124 .Array => |array_info| return array_info.child,127 .Array => |array_info| return array_info.child,
128 .Vector => |vector_info| return vector_info.child,
125 else => {},129 else => {},
126 },130 },
127 .Many, .C, .Slice => return info.child,131 .Many, .C, .Slice => return info.child,
128 },132 },
129 else => {},133 else => {},
130 }134 }
131 @compileError("Expected pointer, slice, or array, found '" ++ @typeName(T) ++ "'");135 @compileError("Expected pointer, slice, array or vector type, found '" ++ @typeName(T) ++ "'");
132}136}
133137
134test "std.meta.Elem" {138test "std.meta.Elem" {
...@@ -136,6 +140,8 @@ test "std.meta.Elem" {...@@ -136,6 +140,8 @@ test "std.meta.Elem" {
136 testing.expect(Elem([*]u8) == u8);140 testing.expect(Elem([*]u8) == u8);
137 testing.expect(Elem([]u8) == u8);141 testing.expect(Elem([]u8) == u8);
138 testing.expect(Elem(*[10]u8) == u8);142 testing.expect(Elem(*[10]u8) == u8);
143 testing.expect(Elem(Vector(2, u8)) == u8);
144 testing.expect(Elem(*Vector(2, u8)) == u8);
139}145}
140146
141/// Given a type which can have a sentinel e.g. `[:0]u8`, returns the sentinel value,147/// Given a type which can have a sentinel e.g. `[:0]u8`, returns the sentinel value,