More vector support in std.meta

master
data-man 2020-05-26 15:52:37 +05:00 committed by Andrew Kelley
parent 4b8077ea8e
commit d10e407977
1 changed files with 9 additions and 3 deletions

View File

@ -102,9 +102,10 @@ test "std.meta.alignment" {
pub fn Child(comptime T: type) type {
return switch (@typeInfo(T)) {
.Array => |info| info.child,
.Vector => |info| info.child,
.Pointer => |info| info.child,
.Optional => |info| info.child,
else => @compileError("Expected pointer, optional, or array type, found '" ++ @typeName(T) ++ "'"),
else => @compileError("Expected pointer, optional, array or vector type, found '" ++ @typeName(T) ++ "'"),
};
}
@ -113,22 +114,25 @@ test "std.meta.Child" {
testing.expect(Child(*u8) == u8);
testing.expect(Child([]u8) == u8);
testing.expect(Child(?u8) == u8);
testing.expect(Child(Vector(2, u8)) == u8);
}
/// Given a "memory span" type, returns the "element type".
pub fn Elem(comptime T: type) type {
switch (@typeInfo(T)) {
.Array => |info| return info.child,
.Array, => |info| return info.child,
.Vector, => |info| return info.child,
.Pointer => |info| switch (info.size) {
.One => switch (@typeInfo(info.child)) {
.Array => |array_info| return array_info.child,
.Vector => |vector_info| return vector_info.child,
else => {},
},
.Many, .C, .Slice => return info.child,
},
else => {},
}
@compileError("Expected pointer, slice, or array, found '" ++ @typeName(T) ++ "'");
@compileError("Expected pointer, slice, array or vector type, found '" ++ @typeName(T) ++ "'");
}
test "std.meta.Elem" {
@ -136,6 +140,8 @@ test "std.meta.Elem" {
testing.expect(Elem([*]u8) == u8);
testing.expect(Elem([]u8) == u8);
testing.expect(Elem(*[10]u8) == u8);
testing.expect(Elem(Vector(2, u8)) == u8);
testing.expect(Elem(*Vector(2, u8)) == u8);
}
/// Given a type which can have a sentinel e.g. `[:0]u8`, returns the sentinel value,