2019-03-02 13:46:04 -08:00
|
|
|
const std = @import("std.zig");
|
2018-10-19 14:19:22 -07:00
|
|
|
const builtin = @import("builtin");
|
|
|
|
const debug = std.debug;
|
|
|
|
const mem = std.mem;
|
|
|
|
const math = std.math;
|
2019-02-08 15:18:47 -08:00
|
|
|
const testing = std.testing;
|
2018-10-19 14:19:22 -07:00
|
|
|
|
2019-03-02 13:46:04 -08:00
|
|
|
pub const trait = @import("meta/trait.zig");
|
2018-10-19 14:19:22 -07:00
|
|
|
|
|
|
|
const TypeInfo = builtin.TypeInfo;
|
|
|
|
|
|
|
|
pub fn tagName(v: var) []const u8 {
|
2019-12-09 12:56:19 -08:00
|
|
|
const T = @TypeOf(v);
|
2018-10-19 14:19:22 -07:00
|
|
|
switch (@typeInfo(T)) {
|
2020-02-13 14:20:40 -08:00
|
|
|
.ErrorSet => return @errorName(v),
|
2019-03-14 08:55:29 -07:00
|
|
|
else => return @tagName(v),
|
2018-10-19 14:19:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
test "std.meta.tagName" {
|
2018-11-13 05:08:37 -08:00
|
|
|
const E1 = enum {
|
2018-10-19 14:19:22 -07:00
|
|
|
A,
|
|
|
|
B,
|
|
|
|
};
|
2018-11-13 05:08:37 -08:00
|
|
|
const E2 = enum(u8) {
|
2018-10-19 14:19:22 -07:00
|
|
|
C = 33,
|
|
|
|
D,
|
|
|
|
};
|
2018-11-13 05:08:37 -08:00
|
|
|
const U1 = union(enum) {
|
2018-10-19 14:19:22 -07:00
|
|
|
G: u8,
|
|
|
|
H: u16,
|
|
|
|
};
|
2018-11-13 05:08:37 -08:00
|
|
|
const U2 = union(E2) {
|
2018-10-19 14:19:22 -07:00
|
|
|
C: u8,
|
|
|
|
D: u16,
|
|
|
|
};
|
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
var u1g = U1{ .G = 0 };
|
|
|
|
var u1h = U1{ .H = 0 };
|
|
|
|
var u2a = U2{ .C = 0 };
|
|
|
|
var u2b = U2{ .D = 0 };
|
2018-10-19 14:19:22 -07:00
|
|
|
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(mem.eql(u8, tagName(E1.A), "A"));
|
|
|
|
testing.expect(mem.eql(u8, tagName(E1.B), "B"));
|
|
|
|
testing.expect(mem.eql(u8, tagName(E2.C), "C"));
|
|
|
|
testing.expect(mem.eql(u8, tagName(E2.D), "D"));
|
|
|
|
testing.expect(mem.eql(u8, tagName(error.E), "E"));
|
|
|
|
testing.expect(mem.eql(u8, tagName(error.F), "F"));
|
|
|
|
testing.expect(mem.eql(u8, tagName(u1g), "G"));
|
|
|
|
testing.expect(mem.eql(u8, tagName(u1h), "H"));
|
|
|
|
testing.expect(mem.eql(u8, tagName(u2a), "C"));
|
|
|
|
testing.expect(mem.eql(u8, tagName(u2b), "D"));
|
2018-10-19 14:19:22 -07:00
|
|
|
}
|
|
|
|
|
2018-11-25 07:08:12 -08:00
|
|
|
pub fn stringToEnum(comptime T: type, str: []const u8) ?T {
|
2020-05-26 22:31:36 -07:00
|
|
|
// Using ComptimeStringMap here is more performant, but it will start to take too
|
|
|
|
// long to compile if the enum is large enough, due to the current limits of comptime
|
|
|
|
// performance when doing things like constructing lookup maps at comptime.
|
|
|
|
// TODO The '100' here is arbitrary and should be increased when possible:
|
|
|
|
// - https://github.com/ziglang/zig/issues/4055
|
|
|
|
// - https://github.com/ziglang/zig/issues/3863
|
|
|
|
if (@typeInfo(T).Enum.fields.len <= 100) {
|
|
|
|
const kvs = comptime build_kvs: {
|
|
|
|
// In order to generate an array of structs that play nice with anonymous
|
|
|
|
// list literals, we need to give them "0" and "1" field names.
|
|
|
|
// TODO https://github.com/ziglang/zig/issues/4335
|
|
|
|
const EnumKV = struct {
|
|
|
|
@"0": []const u8,
|
|
|
|
@"1": T,
|
|
|
|
};
|
|
|
|
var kvs_array: [@typeInfo(T).Enum.fields.len]EnumKV = undefined;
|
|
|
|
inline for (@typeInfo(T).Enum.fields) |enumField, i| {
|
|
|
|
kvs_array[i] = .{.@"0" = enumField.name, .@"1" = @field(T, enumField.name)};
|
|
|
|
}
|
|
|
|
break :build_kvs kvs_array[0..];
|
|
|
|
};
|
|
|
|
const map = std.ComptimeStringMap(T, kvs);
|
|
|
|
return map.get(str);
|
|
|
|
} else {
|
|
|
|
inline for (@typeInfo(T).Enum.fields) |enumField| {
|
|
|
|
if (mem.eql(u8, str, enumField.name)) {
|
|
|
|
return @field(T, enumField.name);
|
|
|
|
}
|
2018-11-25 07:08:12 -08:00
|
|
|
}
|
2020-05-26 22:31:36 -07:00
|
|
|
return null;
|
2018-11-25 07:08:12 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
test "std.meta.stringToEnum" {
|
|
|
|
const E1 = enum {
|
|
|
|
A,
|
|
|
|
B,
|
|
|
|
};
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(E1.A == stringToEnum(E1, "A").?);
|
|
|
|
testing.expect(E1.B == stringToEnum(E1, "B").?);
|
|
|
|
testing.expect(null == stringToEnum(E1, "C"));
|
2018-11-25 07:08:12 -08:00
|
|
|
}
|
|
|
|
|
2018-11-23 08:02:14 -08:00
|
|
|
pub fn bitCount(comptime T: type) comptime_int {
|
2018-10-19 14:19:22 -07:00
|
|
|
return switch (@typeInfo(T)) {
|
2020-02-13 14:20:40 -08:00
|
|
|
.Bool => 1,
|
|
|
|
.Int => |info| info.bits,
|
|
|
|
.Float => |info| info.bits,
|
2019-07-07 08:09:54 -07:00
|
|
|
else => @compileError("Expected bool, int or float type, found '" ++ @typeName(T) ++ "'"),
|
2018-10-19 14:19:22 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
test "std.meta.bitCount" {
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(bitCount(u8) == 8);
|
|
|
|
testing.expect(bitCount(f32) == 32);
|
2018-10-19 14:19:22 -07:00
|
|
|
}
|
|
|
|
|
2018-11-23 08:02:14 -08:00
|
|
|
pub fn alignment(comptime T: type) comptime_int {
|
2018-10-19 14:19:22 -07:00
|
|
|
//@alignOf works on non-pointer types
|
2020-02-13 14:20:40 -08:00
|
|
|
const P = if (comptime trait.is(.Pointer)(T)) T else *T;
|
2018-10-19 14:19:22 -07:00
|
|
|
return @typeInfo(P).Pointer.alignment;
|
|
|
|
}
|
|
|
|
|
|
|
|
test "std.meta.alignment" {
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(alignment(u8) == 1);
|
|
|
|
testing.expect(alignment(*align(1) u8) == 1);
|
|
|
|
testing.expect(alignment(*align(2) u8) == 2);
|
|
|
|
testing.expect(alignment([]align(1) u8) == 1);
|
|
|
|
testing.expect(alignment([]align(2) u8) == 2);
|
2018-10-19 14:19:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn Child(comptime T: type) type {
|
|
|
|
return switch (@typeInfo(T)) {
|
2020-02-13 14:20:40 -08:00
|
|
|
.Array => |info| info.child,
|
2020-05-26 03:52:37 -07:00
|
|
|
.Vector => |info| info.child,
|
2020-02-13 14:20:40 -08:00
|
|
|
.Pointer => |info| info.child,
|
|
|
|
.Optional => |info| info.child,
|
2020-05-26 03:52:37 -07:00
|
|
|
else => @compileError("Expected pointer, optional, array or vector type, found '" ++ @typeName(T) ++ "'"),
|
2018-10-19 14:19:22 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
test "std.meta.Child" {
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(Child([1]u8) == u8);
|
|
|
|
testing.expect(Child(*u8) == u8);
|
|
|
|
testing.expect(Child([]u8) == u8);
|
|
|
|
testing.expect(Child(?u8) == u8);
|
2020-05-26 03:52:37 -07:00
|
|
|
testing.expect(Child(Vector(2, u8)) == u8);
|
2018-10-19 14:19:22 -07:00
|
|
|
}
|
|
|
|
|
2020-03-18 16:29:24 -07:00
|
|
|
/// Given a "memory span" type, returns the "element type".
|
|
|
|
pub fn Elem(comptime T: type) type {
|
|
|
|
switch (@typeInfo(T)) {
|
2020-05-26 03:52:37 -07:00
|
|
|
.Array, => |info| return info.child,
|
|
|
|
.Vector, => |info| return info.child,
|
2020-03-18 16:29:24 -07:00
|
|
|
.Pointer => |info| switch (info.size) {
|
|
|
|
.One => switch (@typeInfo(info.child)) {
|
|
|
|
.Array => |array_info| return array_info.child,
|
2020-05-26 03:52:37 -07:00
|
|
|
.Vector => |vector_info| return vector_info.child,
|
2020-03-18 16:29:24 -07:00
|
|
|
else => {},
|
|
|
|
},
|
|
|
|
.Many, .C, .Slice => return info.child,
|
|
|
|
},
|
|
|
|
else => {},
|
|
|
|
}
|
2020-05-26 03:52:37 -07:00
|
|
|
@compileError("Expected pointer, slice, array or vector type, found '" ++ @typeName(T) ++ "'");
|
2020-03-18 16:29:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
test "std.meta.Elem" {
|
|
|
|
testing.expect(Elem([1]u8) == u8);
|
|
|
|
testing.expect(Elem([*]u8) == u8);
|
|
|
|
testing.expect(Elem([]u8) == u8);
|
|
|
|
testing.expect(Elem(*[10]u8) == u8);
|
2020-05-26 03:52:37 -07:00
|
|
|
testing.expect(Elem(Vector(2, u8)) == u8);
|
|
|
|
testing.expect(Elem(*Vector(2, u8)) == u8);
|
2020-03-18 16:29:24 -07:00
|
|
|
}
|
|
|
|
|
2020-03-17 15:54:09 -07:00
|
|
|
/// Given a type which can have a sentinel e.g. `[:0]u8`, returns the sentinel value,
|
|
|
|
/// or `null` if there is not one.
|
|
|
|
/// Types which cannot possibly have a sentinel will be a compile error.
|
2020-03-18 16:29:24 -07:00
|
|
|
pub fn sentinel(comptime T: type) ?Elem(T) {
|
2019-12-26 19:31:20 -08:00
|
|
|
switch (@typeInfo(T)) {
|
2020-03-17 15:54:09 -07:00
|
|
|
.Array => |info| return info.sentinel,
|
|
|
|
.Pointer => |info| {
|
|
|
|
switch (info.size) {
|
|
|
|
.Many, .Slice => return info.sentinel,
|
2020-03-18 16:29:24 -07:00
|
|
|
.One => switch (@typeInfo(info.child)) {
|
2020-03-17 15:54:09 -07:00
|
|
|
.Array => |array_info| return array_info.sentinel,
|
|
|
|
else => {},
|
2019-12-26 19:31:20 -08:00
|
|
|
},
|
|
|
|
else => {},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
else => {},
|
|
|
|
}
|
2020-03-17 15:54:09 -07:00
|
|
|
@compileError("type '" ++ @typeName(T) ++ "' cannot possibly have a sentinel");
|
2019-12-26 19:31:20 -08:00
|
|
|
}
|
|
|
|
|
2020-03-17 15:54:09 -07:00
|
|
|
test "std.meta.sentinel" {
|
2020-03-18 16:29:24 -07:00
|
|
|
testSentinel();
|
|
|
|
comptime testSentinel();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn testSentinel() void {
|
2020-03-17 15:54:09 -07:00
|
|
|
testing.expectEqual(@as(u8, 0), sentinel([:0]u8).?);
|
|
|
|
testing.expectEqual(@as(u8, 0), sentinel([*:0]u8).?);
|
|
|
|
testing.expectEqual(@as(u8, 0), sentinel([5:0]u8).?);
|
|
|
|
testing.expectEqual(@as(u8, 0), sentinel(*const [5:0]u8).?);
|
|
|
|
|
|
|
|
testing.expect(sentinel([]u8) == null);
|
|
|
|
testing.expect(sentinel([*]u8) == null);
|
|
|
|
testing.expect(sentinel([5]u8) == null);
|
|
|
|
testing.expect(sentinel(*const [5]u8) == null);
|
2019-12-26 19:31:20 -08:00
|
|
|
}
|
|
|
|
|
2018-10-19 14:19:22 -07:00
|
|
|
pub fn containerLayout(comptime T: type) TypeInfo.ContainerLayout {
|
|
|
|
return switch (@typeInfo(T)) {
|
2020-02-13 14:20:40 -08:00
|
|
|
.Struct => |info| info.layout,
|
|
|
|
.Enum => |info| info.layout,
|
|
|
|
.Union => |info| info.layout,
|
2018-10-19 14:27:16 -07:00
|
|
|
else => @compileError("Expected struct, enum or union type, found '" ++ @typeName(T) ++ "'"),
|
2018-10-19 14:19:22 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
test "std.meta.containerLayout" {
|
2018-11-13 05:08:37 -08:00
|
|
|
const E1 = enum {
|
2018-10-19 14:19:22 -07:00
|
|
|
A,
|
|
|
|
};
|
2018-11-13 05:08:37 -08:00
|
|
|
const E2 = packed enum {
|
2018-10-19 14:19:22 -07:00
|
|
|
A,
|
|
|
|
};
|
2018-11-13 05:08:37 -08:00
|
|
|
const E3 = extern enum {
|
2018-10-19 14:19:22 -07:00
|
|
|
A,
|
|
|
|
};
|
2018-11-13 05:08:37 -08:00
|
|
|
const S1 = struct {};
|
|
|
|
const S2 = packed struct {};
|
|
|
|
const S3 = extern struct {};
|
|
|
|
const U1 = union {
|
2018-10-19 14:19:22 -07:00
|
|
|
a: u8,
|
|
|
|
};
|
2018-11-13 05:08:37 -08:00
|
|
|
const U2 = packed union {
|
2018-10-19 14:19:22 -07:00
|
|
|
a: u8,
|
|
|
|
};
|
2018-11-13 05:08:37 -08:00
|
|
|
const U3 = extern union {
|
2018-10-19 14:19:22 -07:00
|
|
|
a: u8,
|
|
|
|
};
|
|
|
|
|
2020-02-13 14:20:40 -08:00
|
|
|
testing.expect(containerLayout(E1) == .Auto);
|
|
|
|
testing.expect(containerLayout(E2) == .Packed);
|
|
|
|
testing.expect(containerLayout(E3) == .Extern);
|
|
|
|
testing.expect(containerLayout(S1) == .Auto);
|
|
|
|
testing.expect(containerLayout(S2) == .Packed);
|
|
|
|
testing.expect(containerLayout(S3) == .Extern);
|
|
|
|
testing.expect(containerLayout(U1) == .Auto);
|
|
|
|
testing.expect(containerLayout(U2) == .Packed);
|
|
|
|
testing.expect(containerLayout(U3) == .Extern);
|
2018-10-19 14:19:22 -07:00
|
|
|
}
|
|
|
|
|
2019-05-26 14:22:45 -07:00
|
|
|
pub fn declarations(comptime T: type) []TypeInfo.Declaration {
|
2018-10-19 14:19:22 -07:00
|
|
|
return switch (@typeInfo(T)) {
|
2020-02-13 14:20:40 -08:00
|
|
|
.Struct => |info| info.decls,
|
|
|
|
.Enum => |info| info.decls,
|
|
|
|
.Union => |info| info.decls,
|
2018-10-19 14:27:16 -07:00
|
|
|
else => @compileError("Expected struct, enum or union type, found '" ++ @typeName(T) ++ "'"),
|
2018-10-19 14:19:22 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-05-26 14:22:45 -07:00
|
|
|
test "std.meta.declarations" {
|
2018-11-13 05:08:37 -08:00
|
|
|
const E1 = enum {
|
2018-10-19 14:19:22 -07:00
|
|
|
A,
|
|
|
|
|
|
|
|
fn a() void {}
|
|
|
|
};
|
2018-11-13 05:08:37 -08:00
|
|
|
const S1 = struct {
|
2018-10-19 14:19:22 -07:00
|
|
|
fn a() void {}
|
|
|
|
};
|
2018-11-13 05:08:37 -08:00
|
|
|
const U1 = union {
|
2018-10-19 14:19:22 -07:00
|
|
|
a: u8,
|
|
|
|
|
|
|
|
fn a() void {}
|
|
|
|
};
|
|
|
|
|
2019-06-09 16:24:24 -07:00
|
|
|
const decls = comptime [_][]TypeInfo.Declaration{
|
2019-05-26 14:22:45 -07:00
|
|
|
declarations(E1),
|
|
|
|
declarations(S1),
|
|
|
|
declarations(U1),
|
2018-10-19 14:19:22 -07:00
|
|
|
};
|
|
|
|
|
2019-05-26 14:22:45 -07:00
|
|
|
inline for (decls) |decl| {
|
|
|
|
testing.expect(decl.len == 1);
|
|
|
|
testing.expect(comptime mem.eql(u8, decl[0].name, "a"));
|
2018-10-19 14:19:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 14:22:45 -07:00
|
|
|
pub fn declarationInfo(comptime T: type, comptime decl_name: []const u8) TypeInfo.Declaration {
|
|
|
|
inline for (comptime declarations(T)) |decl| {
|
|
|
|
if (comptime mem.eql(u8, decl.name, decl_name))
|
|
|
|
return decl;
|
2018-10-19 14:19:22 -07:00
|
|
|
}
|
|
|
|
|
2019-05-26 14:22:45 -07:00
|
|
|
@compileError("'" ++ @typeName(T) ++ "' has no declaration '" ++ decl_name ++ "'");
|
2018-10-19 14:19:22 -07:00
|
|
|
}
|
|
|
|
|
2019-05-26 14:22:45 -07:00
|
|
|
test "std.meta.declarationInfo" {
|
2018-11-13 05:08:37 -08:00
|
|
|
const E1 = enum {
|
2018-10-19 14:19:22 -07:00
|
|
|
A,
|
|
|
|
|
|
|
|
fn a() void {}
|
|
|
|
};
|
2018-11-13 05:08:37 -08:00
|
|
|
const S1 = struct {
|
2018-10-19 14:19:22 -07:00
|
|
|
fn a() void {}
|
|
|
|
};
|
2018-11-13 05:08:37 -08:00
|
|
|
const U1 = union {
|
2018-10-19 14:19:22 -07:00
|
|
|
a: u8,
|
|
|
|
|
|
|
|
fn a() void {}
|
|
|
|
};
|
|
|
|
|
2019-06-09 16:24:24 -07:00
|
|
|
const infos = comptime [_]TypeInfo.Declaration{
|
2019-05-26 14:22:45 -07:00
|
|
|
declarationInfo(E1, "a"),
|
|
|
|
declarationInfo(S1, "a"),
|
|
|
|
declarationInfo(U1, "a"),
|
2018-10-19 14:19:22 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
inline for (infos) |info| {
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(comptime mem.eql(u8, info.name, "a"));
|
|
|
|
testing.expect(!info.is_pub);
|
2018-10-19 14:19:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fields(comptime T: type) switch (@typeInfo(T)) {
|
2020-02-13 14:20:40 -08:00
|
|
|
.Struct => []TypeInfo.StructField,
|
|
|
|
.Union => []TypeInfo.UnionField,
|
|
|
|
.ErrorSet => []TypeInfo.Error,
|
|
|
|
.Enum => []TypeInfo.EnumField,
|
2018-10-19 14:27:16 -07:00
|
|
|
else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"),
|
2018-10-19 14:19:22 -07:00
|
|
|
} {
|
|
|
|
return switch (@typeInfo(T)) {
|
2020-02-13 14:20:40 -08:00
|
|
|
.Struct => |info| info.fields,
|
|
|
|
.Union => |info| info.fields,
|
|
|
|
.Enum => |info| info.fields,
|
|
|
|
.ErrorSet => |errors| errors.?, // must be non global error set
|
2018-10-19 14:27:16 -07:00
|
|
|
else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"),
|
2018-10-19 14:19:22 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
test "std.meta.fields" {
|
2018-11-13 05:08:37 -08:00
|
|
|
const E1 = enum {
|
2018-10-19 14:19:22 -07:00
|
|
|
A,
|
|
|
|
};
|
2018-11-13 05:08:37 -08:00
|
|
|
const E2 = error{A};
|
|
|
|
const S1 = struct {
|
2018-10-19 14:19:22 -07:00
|
|
|
a: u8,
|
|
|
|
};
|
2018-11-13 05:08:37 -08:00
|
|
|
const U1 = union {
|
2018-10-19 14:19:22 -07:00
|
|
|
a: u8,
|
|
|
|
};
|
|
|
|
|
|
|
|
const e1f = comptime fields(E1);
|
|
|
|
const e2f = comptime fields(E2);
|
|
|
|
const sf = comptime fields(S1);
|
|
|
|
const uf = comptime fields(U1);
|
|
|
|
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(e1f.len == 1);
|
|
|
|
testing.expect(e2f.len == 1);
|
|
|
|
testing.expect(sf.len == 1);
|
|
|
|
testing.expect(uf.len == 1);
|
|
|
|
testing.expect(mem.eql(u8, e1f[0].name, "A"));
|
|
|
|
testing.expect(mem.eql(u8, e2f[0].name, "A"));
|
|
|
|
testing.expect(mem.eql(u8, sf[0].name, "a"));
|
|
|
|
testing.expect(mem.eql(u8, uf[0].name, "a"));
|
|
|
|
testing.expect(comptime sf[0].field_type == u8);
|
|
|
|
testing.expect(comptime uf[0].field_type == u8);
|
2018-10-19 14:19:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fieldInfo(comptime T: type, comptime field_name: []const u8) switch (@typeInfo(T)) {
|
2020-02-13 14:20:40 -08:00
|
|
|
.Struct => TypeInfo.StructField,
|
|
|
|
.Union => TypeInfo.UnionField,
|
|
|
|
.ErrorSet => TypeInfo.Error,
|
|
|
|
.Enum => TypeInfo.EnumField,
|
2018-10-19 14:27:16 -07:00
|
|
|
else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"),
|
2018-10-19 14:19:22 -07:00
|
|
|
} {
|
|
|
|
inline for (comptime fields(T)) |field| {
|
|
|
|
if (comptime mem.eql(u8, field.name, field_name))
|
|
|
|
return field;
|
|
|
|
}
|
|
|
|
|
|
|
|
@compileError("'" ++ @typeName(T) ++ "' has no field '" ++ field_name ++ "'");
|
|
|
|
}
|
|
|
|
|
|
|
|
test "std.meta.fieldInfo" {
|
2018-11-13 05:08:37 -08:00
|
|
|
const E1 = enum {
|
2018-10-19 14:19:22 -07:00
|
|
|
A,
|
|
|
|
};
|
2018-11-13 05:08:37 -08:00
|
|
|
const E2 = error{A};
|
|
|
|
const S1 = struct {
|
2018-10-19 14:19:22 -07:00
|
|
|
a: u8,
|
|
|
|
};
|
2018-11-13 05:08:37 -08:00
|
|
|
const U1 = union {
|
2018-10-19 14:19:22 -07:00
|
|
|
a: u8,
|
|
|
|
};
|
|
|
|
|
|
|
|
const e1f = comptime fieldInfo(E1, "A");
|
|
|
|
const e2f = comptime fieldInfo(E2, "A");
|
|
|
|
const sf = comptime fieldInfo(S1, "a");
|
|
|
|
const uf = comptime fieldInfo(U1, "a");
|
|
|
|
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(mem.eql(u8, e1f.name, "A"));
|
|
|
|
testing.expect(mem.eql(u8, e2f.name, "A"));
|
|
|
|
testing.expect(mem.eql(u8, sf.name, "a"));
|
|
|
|
testing.expect(mem.eql(u8, uf.name, "a"));
|
|
|
|
testing.expect(comptime sf.field_type == u8);
|
|
|
|
testing.expect(comptime uf.field_type == u8);
|
2018-10-19 14:19:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn TagType(comptime T: type) type {
|
|
|
|
return switch (@typeInfo(T)) {
|
2020-02-13 14:20:40 -08:00
|
|
|
.Enum => |info| info.tag_type,
|
|
|
|
.Union => |info| if (info.tag_type) |Tag| Tag else null,
|
2018-10-19 14:19:22 -07:00
|
|
|
else => @compileError("expected enum or union type, found '" ++ @typeName(T) ++ "'"),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
test "std.meta.TagType" {
|
2018-11-13 05:08:37 -08:00
|
|
|
const E = enum(u8) {
|
2018-10-19 14:19:22 -07:00
|
|
|
C = 33,
|
|
|
|
D,
|
|
|
|
};
|
2018-11-13 05:08:37 -08:00
|
|
|
const U = union(E) {
|
2018-10-19 14:19:22 -07:00
|
|
|
C: u8,
|
|
|
|
D: u16,
|
|
|
|
};
|
|
|
|
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(TagType(E) == u8);
|
|
|
|
testing.expect(TagType(U) == E);
|
2018-10-19 14:19:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
///Returns the active tag of a tagged union
|
2019-12-09 12:56:19 -08:00
|
|
|
pub fn activeTag(u: var) @TagType(@TypeOf(u)) {
|
|
|
|
const T = @TypeOf(u);
|
2019-11-07 15:52:09 -08:00
|
|
|
return @as(@TagType(T), u);
|
2018-10-19 14:19:22 -07:00
|
|
|
}
|
|
|
|
|
2018-10-19 14:27:16 -07:00
|
|
|
test "std.meta.activeTag" {
|
2018-11-13 05:08:37 -08:00
|
|
|
const UE = enum {
|
2018-10-19 14:19:22 -07:00
|
|
|
Int,
|
|
|
|
Float,
|
|
|
|
};
|
2018-10-19 14:27:16 -07:00
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const U = union(UE) {
|
2018-10-19 14:19:22 -07:00
|
|
|
Int: u32,
|
|
|
|
Float: f32,
|
|
|
|
};
|
2018-10-19 14:27:16 -07:00
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
var u = U{ .Int = 32 };
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(activeTag(u) == UE.Int);
|
2018-10-19 14:19:22 -07:00
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
u = U{ .Float = 112.9876 };
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(activeTag(u) == UE.Float);
|
2018-10-19 14:19:22 -07:00
|
|
|
}
|
|
|
|
|
2018-11-23 08:02:14 -08:00
|
|
|
///Given a tagged union type, and an enum, return the type of the union
|
|
|
|
/// field corresponding to the enum tag.
|
2019-12-30 04:09:18 -08:00
|
|
|
pub fn TagPayloadType(comptime U: type, tag: @TagType(U)) type {
|
2020-02-13 14:20:40 -08:00
|
|
|
testing.expect(trait.is(.Union)(U));
|
2018-11-23 08:02:14 -08:00
|
|
|
|
|
|
|
const info = @typeInfo(U).Union;
|
|
|
|
|
|
|
|
inline for (info.fields) |field_info| {
|
|
|
|
if (field_info.enum_field.?.value == @enumToInt(tag)) return field_info.field_type;
|
|
|
|
}
|
|
|
|
unreachable;
|
|
|
|
}
|
|
|
|
|
|
|
|
test "std.meta.TagPayloadType" {
|
|
|
|
const Event = union(enum) {
|
|
|
|
Moved: struct {
|
|
|
|
from: i32,
|
|
|
|
to: i32,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const MovedEvent = TagPayloadType(Event, Event.Moved);
|
|
|
|
var e: Event = undefined;
|
2019-12-09 12:56:19 -08:00
|
|
|
testing.expect(MovedEvent == @TypeOf(e.Moved));
|
2018-11-23 08:02:14 -08:00
|
|
|
}
|
|
|
|
|
2020-02-13 14:20:40 -08:00
|
|
|
/// Compares two of any type for equality. Containers are compared on a field-by-field basis,
|
2018-10-19 14:19:22 -07:00
|
|
|
/// where possible. Pointers are not followed.
|
2019-12-09 12:56:19 -08:00
|
|
|
pub fn eql(a: var, b: @TypeOf(a)) bool {
|
|
|
|
const T = @TypeOf(a);
|
2018-10-19 14:27:16 -07:00
|
|
|
|
2020-02-13 14:20:40 -08:00
|
|
|
switch (@typeInfo(T)) {
|
|
|
|
.Struct => |info| {
|
2018-10-19 14:27:16 -07:00
|
|
|
inline for (info.fields) |field_info| {
|
|
|
|
if (!eql(@field(a, field_info.name), @field(b, field_info.name))) return false;
|
2018-10-19 14:19:22 -07:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
2020-02-13 14:20:40 -08:00
|
|
|
.ErrorUnion => {
|
2018-10-19 14:27:16 -07:00
|
|
|
if (a) |a_p| {
|
|
|
|
if (b) |b_p| return eql(a_p, b_p) else |_| return false;
|
|
|
|
} else |a_e| {
|
|
|
|
if (b) |_| return false else |b_e| return a_e == b_e;
|
2018-10-19 14:19:22 -07:00
|
|
|
}
|
|
|
|
},
|
2020-02-13 14:20:40 -08:00
|
|
|
.Union => |info| {
|
2018-10-19 14:27:16 -07:00
|
|
|
if (info.tag_type) |_| {
|
2018-10-19 14:19:22 -07:00
|
|
|
const tag_a = activeTag(a);
|
|
|
|
const tag_b = activeTag(b);
|
2018-10-19 14:27:16 -07:00
|
|
|
if (tag_a != tag_b) return false;
|
|
|
|
|
|
|
|
inline for (info.fields) |field_info| {
|
2018-10-19 14:19:22 -07:00
|
|
|
const enum_field = field_info.enum_field.?;
|
2018-10-19 14:27:16 -07:00
|
|
|
if (enum_field.value == @enumToInt(tag_a)) {
|
|
|
|
return eql(@field(a, enum_field.name), @field(b, enum_field.name));
|
2018-10-19 14:19:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-19 14:27:16 -07:00
|
|
|
|
2018-10-19 14:19:22 -07:00
|
|
|
@compileError("cannot compare untagged union type " ++ @typeName(T));
|
|
|
|
},
|
2020-02-13 14:20:40 -08:00
|
|
|
.Array => {
|
2018-10-19 14:27:16 -07:00
|
|
|
if (a.len != b.len) return false;
|
|
|
|
for (a) |e, i|
|
|
|
|
if (!eql(e, b[i])) return false;
|
2018-10-19 14:19:22 -07:00
|
|
|
return true;
|
|
|
|
},
|
2020-02-13 14:20:40 -08:00
|
|
|
.Vector => |info| {
|
2020-01-07 06:14:37 -08:00
|
|
|
var i: usize = 0;
|
|
|
|
while (i < info.len) : (i += 1) {
|
|
|
|
if (!eql(a[i], b[i])) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
2020-02-13 14:20:40 -08:00
|
|
|
.Pointer => |info| {
|
|
|
|
return switch (info.size) {
|
2020-02-18 12:34:13 -08:00
|
|
|
.One, .Many, .C => a == b,
|
2020-02-13 14:20:40 -08:00
|
|
|
.Slice => a.ptr == b.ptr and a.len == b.len,
|
|
|
|
};
|
2018-10-19 14:19:22 -07:00
|
|
|
},
|
2020-02-13 14:20:40 -08:00
|
|
|
.Optional => {
|
2019-02-11 13:07:40 -08:00
|
|
|
if (a == null and b == null) return true;
|
|
|
|
if (a == null or b == null) return false;
|
2018-11-23 08:02:14 -08:00
|
|
|
return eql(a.?, b.?);
|
|
|
|
},
|
2018-10-19 14:19:22 -07:00
|
|
|
else => return a == b,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-19 14:27:16 -07:00
|
|
|
test "std.meta.eql" {
|
2018-11-13 05:08:37 -08:00
|
|
|
const S = struct {
|
2018-10-19 14:19:22 -07:00
|
|
|
a: u32,
|
|
|
|
b: f64,
|
|
|
|
c: [5]u8,
|
|
|
|
};
|
2018-10-19 14:27:16 -07:00
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const U = union(enum) {
|
2018-10-19 14:19:22 -07:00
|
|
|
s: S,
|
2018-11-23 08:02:14 -08:00
|
|
|
f: ?f32,
|
2018-10-19 14:19:22 -07:00
|
|
|
};
|
2018-10-19 14:27:16 -07:00
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const s_1 = S{
|
2018-10-19 14:19:22 -07:00
|
|
|
.a = 134,
|
|
|
|
.b = 123.3,
|
2019-11-19 17:29:08 -08:00
|
|
|
.c = "12345".*,
|
2018-10-19 14:19:22 -07:00
|
|
|
};
|
2018-10-19 14:27:16 -07:00
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const s_2 = S{
|
2018-10-19 14:19:22 -07:00
|
|
|
.a = 1,
|
|
|
|
.b = 123.3,
|
2019-11-19 17:29:08 -08:00
|
|
|
.c = "54321".*,
|
2018-10-19 14:19:22 -07:00
|
|
|
};
|
2018-10-19 14:27:16 -07:00
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const s_3 = S{
|
2018-10-19 14:19:22 -07:00
|
|
|
.a = 134,
|
|
|
|
.b = 123.3,
|
2019-11-19 17:29:08 -08:00
|
|
|
.c = "12345".*,
|
2018-10-19 14:19:22 -07:00
|
|
|
};
|
2018-10-19 14:27:16 -07:00
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const u_1 = U{ .f = 24 };
|
|
|
|
const u_2 = U{ .s = s_1 };
|
|
|
|
const u_3 = U{ .f = 24 };
|
2018-10-19 14:27:16 -07:00
|
|
|
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(eql(s_1, s_3));
|
|
|
|
testing.expect(eql(&s_1, &s_1));
|
|
|
|
testing.expect(!eql(&s_1, &s_3));
|
|
|
|
testing.expect(eql(u_1, u_3));
|
|
|
|
testing.expect(!eql(u_1, u_2));
|
2018-10-19 14:27:16 -07:00
|
|
|
|
2019-11-19 17:29:08 -08:00
|
|
|
var a1 = "abcdef".*;
|
|
|
|
var a2 = "abcdef".*;
|
|
|
|
var a3 = "ghijkl".*;
|
2018-10-19 14:27:16 -07:00
|
|
|
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(eql(a1, a2));
|
|
|
|
testing.expect(!eql(a1, a3));
|
|
|
|
testing.expect(!eql(a1[0..], a2[0..]));
|
2018-10-19 14:27:16 -07:00
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const EU = struct {
|
2018-10-19 14:27:16 -07:00
|
|
|
fn tst(err: bool) !u8 {
|
|
|
|
if (err) return error.Error;
|
2019-11-06 20:25:57 -08:00
|
|
|
return @as(u8, 5);
|
2018-10-19 14:19:22 -07:00
|
|
|
}
|
|
|
|
};
|
2018-10-19 14:27:16 -07:00
|
|
|
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(eql(EU.tst(true), EU.tst(true)));
|
|
|
|
testing.expect(eql(EU.tst(false), EU.tst(false)));
|
|
|
|
testing.expect(!eql(EU.tst(false), EU.tst(true)));
|
2020-01-07 06:14:37 -08:00
|
|
|
|
|
|
|
var v1 = @splat(4, @as(u32, 1));
|
|
|
|
var v2 = @splat(4, @as(u32, 1));
|
|
|
|
var v3 = @splat(4, @as(u32, 2));
|
|
|
|
|
|
|
|
testing.expect(eql(v1, v2));
|
|
|
|
testing.expect(!eql(v1, v3));
|
2018-10-19 14:27:16 -07:00
|
|
|
}
|
2018-11-18 17:18:24 -08:00
|
|
|
|
|
|
|
test "intToEnum with error return" {
|
|
|
|
const E1 = enum {
|
|
|
|
A,
|
|
|
|
};
|
|
|
|
const E2 = enum {
|
|
|
|
A,
|
|
|
|
B,
|
|
|
|
};
|
|
|
|
|
|
|
|
var zero: u8 = 0;
|
|
|
|
var one: u16 = 1;
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(intToEnum(E1, zero) catch unreachable == E1.A);
|
|
|
|
testing.expect(intToEnum(E2, one) catch unreachable == E2.B);
|
|
|
|
testing.expectError(error.InvalidEnumTag, intToEnum(E1, one));
|
2018-11-18 17:18:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub const IntToEnumError = error{InvalidEnumTag};
|
|
|
|
|
|
|
|
pub fn intToEnum(comptime Tag: type, tag_int: var) IntToEnumError!Tag {
|
2020-02-24 13:03:30 -08:00
|
|
|
inline for (@typeInfo(Tag).Enum.fields) |f| {
|
|
|
|
const this_tag_value = @field(Tag, f.name);
|
2018-11-18 17:18:24 -08:00
|
|
|
if (tag_int == @enumToInt(this_tag_value)) {
|
|
|
|
return this_tag_value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return error.InvalidEnumTag;
|
|
|
|
}
|
2019-10-15 22:29:16 -07:00
|
|
|
|
|
|
|
/// Given a type and a name, return the field index according to source order.
|
|
|
|
/// Returns `null` if the field is not found.
|
|
|
|
pub fn fieldIndex(comptime T: type, comptime name: []const u8) ?comptime_int {
|
|
|
|
inline for (fields(T)) |field, i| {
|
|
|
|
if (mem.eql(u8, field.name, name))
|
2019-12-05 10:20:38 -08:00
|
|
|
return i;
|
2019-10-15 22:29:16 -07:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2019-10-16 16:16:39 -07:00
|
|
|
|
|
|
|
/// Given a type, reference all the declarations inside, so that the semantic analyzer sees them.
|
|
|
|
pub fn refAllDecls(comptime T: type) void {
|
|
|
|
if (!builtin.is_test) return;
|
2020-02-18 12:34:13 -08:00
|
|
|
inline for (declarations(T)) |decl| {
|
|
|
|
_ = decl;
|
|
|
|
}
|
2019-10-16 16:16:39 -07:00
|
|
|
}
|
2020-01-18 23:40:35 -08:00
|
|
|
|
|
|
|
/// Returns a slice of pointers to public declarations of a namespace.
|
|
|
|
pub fn declList(comptime Namespace: type, comptime Decl: type) []const *const Decl {
|
|
|
|
const S = struct {
|
|
|
|
fn declNameLessThan(lhs: *const Decl, rhs: *const Decl) bool {
|
|
|
|
return mem.lessThan(u8, lhs.name, rhs.name);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
comptime {
|
|
|
|
const decls = declarations(Namespace);
|
|
|
|
var array: [decls.len]*const Decl = undefined;
|
|
|
|
for (decls) |decl, i| {
|
|
|
|
array[i] = &@field(Namespace, decl.name);
|
|
|
|
}
|
|
|
|
std.sort.sort(*const Decl, &array, S.declNameLessThan);
|
|
|
|
return &array;
|
|
|
|
}
|
|
|
|
}
|
2020-02-24 13:39:03 -08:00
|
|
|
|
2020-04-28 18:06:48 -07:00
|
|
|
/// Deprecated: use Int
|
|
|
|
pub const IntType = Int;
|
|
|
|
|
|
|
|
pub fn Int(comptime is_signed: bool, comptime bit_count: u16) type {
|
2020-02-24 13:39:03 -08:00
|
|
|
return @Type(TypeInfo{
|
|
|
|
.Int = .{
|
|
|
|
.is_signed = is_signed,
|
|
|
|
.bits = bit_count,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2020-04-27 22:45:54 -07:00
|
|
|
|
|
|
|
pub fn Vector(comptime len: u32, comptime child: type) type {
|
|
|
|
return @Type(TypeInfo{
|
|
|
|
.Vector = .{
|
|
|
|
.len = len,
|
|
|
|
.child = child,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|