From db3b768eabaf0ca7f83cccabfaa61eb0aa400c41 Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Fri, 19 Oct 2018 23:27:16 +0200 Subject: [PATCH] Ran fmt on last PR --- std/mem.zig | 134 ++++++++++------------ std/meta/index.zig | 184 ++++++++++++----------------- std/meta/trait.zig | 280 ++++++++++++++++----------------------------- 3 files changed, 233 insertions(+), 365 deletions(-) diff --git a/std/mem.zig b/std/mem.zig index ca93e0d2d1..ca96187479 100644 --- a/std/mem.zig +++ b/std/mem.zig @@ -866,128 +866,123 @@ test "std.mem.endianSwap" { assert(endianSwap(u32, 0xDEADBEEF) == 0xEFBEADDE); } - - -fn AsBytesReturnType(comptime P: type) type -{ - if(comptime !trait.isSingleItemPtr(P)) @compileError("expected single item " - ++ "pointer, passed " ++ @typeName(P)); +fn AsBytesReturnType(comptime P: type) type { + if (comptime !trait.isSingleItemPtr(P)) + @compileError("expected single item " ++ "pointer, passed " ++ @typeName(P)); const size = usize(@sizeOf(meta.Child(P))); const alignment = comptime meta.alignment(P); - if(comptime trait.isConstPtr(P)) return *align(alignment) const [size]u8; + + if (comptime trait.isConstPtr(P)) + return *align(alignment) const [size]u8; return *align(alignment) [size]u8; } ///Given a pointer to a single item, returns a slice of the underlying bytes, preserving constness. -pub fn asBytes(ptr: var) AsBytesReturnType(@typeOf(ptr)) -{ +pub fn asBytes(ptr: var) AsBytesReturnType(@typeOf(ptr)) { const P = @typeOf(ptr); return @ptrCast(AsBytesReturnType(P), ptr); } -test "std.mem.asBytes" -{ +test "std.mem.asBytes" { const deadbeef = u32(0xDEADBEEF); - const deadbeef_bytes = switch(builtin.endian) - { + const deadbeef_bytes = switch (builtin.endian) { builtin.Endian.Big => "\xDE\xAD\xBE\xEF", builtin.Endian.Little => "\xEF\xBE\xAD\xDE", }; - + debug.assert(std.mem.eql(u8, asBytes(&deadbeef), deadbeef_bytes)); - + var codeface = u32(0xC0DEFACE); - for(asBytes(&codeface).*) |*b| b.* = 0; + for (asBytes(&codeface).*) |*b| + b.* = 0; debug.assert(codeface == 0); - - const S = packed struct. - { + + const S = packed struct.{ a: u8, b: u8, c: u8, d: u8, }; - - const inst = S.{ .a = 0xBE, .b = 0xEF, .c = 0xDE, .d = 0xA1, }; + + const inst = S.{ + .a = 0xBE, + .b = 0xEF, + .c = 0xDE, + .d = 0xA1, + }; debug.assert(std.mem.eql(u8, asBytes(&inst), "\xBE\xEF\xDE\xA1")); } ///Given any value, returns a copy of its bytes in an array. -pub fn toBytes(value: var) [@sizeOf(@typeOf(value))]u8 -{ +pub fn toBytes(value: var) [@sizeOf(@typeOf(value))]u8 { return asBytes(&value).*; } -test "std.mem.toBytes" -{ +test "std.mem.toBytes" { var my_bytes = toBytes(u32(0x12345678)); - switch(builtin.endian) - { + switch (builtin.endian) { builtin.Endian.Big => debug.assert(std.mem.eql(u8, my_bytes, "\x12\x34\x56\x78")), builtin.Endian.Little => debug.assert(std.mem.eql(u8, my_bytes, "\x78\x56\x34\x12")), } - + my_bytes[0] = '\x99'; - switch(builtin.endian) - { + switch (builtin.endian) { builtin.Endian.Big => debug.assert(std.mem.eql(u8, my_bytes, "\x99\x34\x56\x78")), builtin.Endian.Little => debug.assert(std.mem.eql(u8, my_bytes, "\x99\x56\x34\x12")), } } - -fn BytesAsValueReturnType(comptime T: type, comptime B: type) type -{ +fn BytesAsValueReturnType(comptime T: type, comptime B: type) type { const size = usize(@sizeOf(T)); - if(comptime !trait.is(builtin.TypeId.Pointer)(B) or meta.Child(B) != [size]u8) - { + if (comptime !trait.is(builtin.TypeId.Pointer)(B) or meta.Child(B) != [size]u8) { @compileError("expected *[N]u8 " ++ ", passed " ++ @typeName(B)); } - + const alignment = comptime meta.alignment(B); - - return if(comptime trait.isConstPtr(B)) *align(alignment) const T else *align(alignment) T; + + return if (comptime trait.isConstPtr(B)) *align(alignment) const T else *align(alignment) T; } ///Given a pointer to an array of bytes, returns a pointer to a value of the specified type /// backed by those bytes, preserving constness. -pub fn bytesAsValue(comptime T: type, bytes: var) BytesAsValueReturnType(T, @typeOf(bytes)) -{ +pub fn bytesAsValue(comptime T: type, bytes: var) BytesAsValueReturnType(T, @typeOf(bytes)) { return @ptrCast(BytesAsValueReturnType(T, @typeOf(bytes)), bytes); } -test "std.mem.bytesAsValue" -{ +test "std.mem.bytesAsValue" { const deadbeef = u32(0xDEADBEEF); - const deadbeef_bytes = switch(builtin.endian) - { + const deadbeef_bytes = switch (builtin.endian) { builtin.Endian.Big => "\xDE\xAD\xBE\xEF", builtin.Endian.Little => "\xEF\xBE\xAD\xDE", }; - + debug.assert(deadbeef == bytesAsValue(u32, &deadbeef_bytes).*); - - var codeface_bytes = switch(builtin.endian) - { + + var codeface_bytes = switch (builtin.endian) { builtin.Endian.Big => "\xC0\xDE\xFA\xCE", builtin.Endian.Little => "\xCE\xFA\xDE\xC0", }; var codeface = bytesAsValue(u32, &codeface_bytes); debug.assert(codeface.* == 0xC0DEFACE); codeface.* = 0; - for(codeface_bytes) |b| debug.assert(b == 0); - - const S = packed struct. - { + for (codeface_bytes) |b| + debug.assert(b == 0); + + const S = packed struct.{ a: u8, b: u8, c: u8, d: u8, }; - - const inst = S.{ .a = 0xBE, .b = 0xEF, .c = 0xDE, .d = 0xA1, }; + + const inst = S.{ + .a = 0xBE, + .b = 0xEF, + .c = 0xDE, + .d = 0xA1, + }; const inst_bytes = "\xBE\xEF\xDE\xA1"; const inst2 = bytesAsValue(S, &inst_bytes); debug.assert(meta.eql(inst, inst2.*)); @@ -995,50 +990,43 @@ test "std.mem.bytesAsValue" ///Given a pointer to an array of bytes, returns a value of the specified type backed by a /// copy of those bytes. -pub fn bytesToValue(comptime T: type, bytes: var) T -{ +pub fn bytesToValue(comptime T: type, bytes: var) T { return bytesAsValue(T, &bytes).*; } - test "std.mem.bytesToValue" -{ - const deadbeef_bytes = switch(builtin.endian) - { +test "std.mem.bytesToValue" { + const deadbeef_bytes = switch (builtin.endian) { builtin.Endian.Big => "\xDE\xAD\xBE\xEF", builtin.Endian.Little => "\xEF\xBE\xAD\xDE", }; - + const deadbeef = bytesToValue(u32, deadbeef_bytes); debug.assert(deadbeef == u32(0xDEADBEEF)); } - -fn SubArrayPtrReturnType(comptime T: type, comptime length: usize) type -{ - if(trait.isConstPtr(T)) return *const [length]meta.Child(meta.Child(T)); +fn SubArrayPtrReturnType(comptime T: type, comptime length: usize) type { + if (trait.isConstPtr(T)) + return *const [length]meta.Child(meta.Child(T)); return *[length]meta.Child(meta.Child(T)); } ///Given a pointer to an array, returns a pointer to a portion of that array, preserving constness. -pub fn subArrayPtr(ptr: var, comptime start: usize, comptime length: usize) - SubArrayPtrReturnType(@typeOf(ptr), length) -{ +pub fn subArrayPtr(ptr: var, comptime start: usize, comptime length: usize) SubArrayPtrReturnType(@typeOf(ptr), length) { debug.assert(start + length <= ptr.*.len); - + const ReturnType = SubArrayPtrReturnType(@typeOf(ptr), length); const T = meta.Child(meta.Child(@typeOf(ptr))); return @ptrCast(ReturnType, &ptr[start]); } -test "std.mem.subArrayPtr" -{ +test "std.mem.subArrayPtr" { const a1 = "abcdef"; const sub1 = subArrayPtr(&a1, 2, 3); debug.assert(std.mem.eql(u8, sub1.*, "cde")); - + var a2 = "abcdef"; var sub2 = subArrayPtr(&a2, 2, 3); debug.assert(std.mem.eql(u8, sub2, "cde")); sub2[1] = 'X'; debug.assert(std.mem.eql(u8, a2, "abcXef")); -} \ No newline at end of file +} diff --git a/std/meta/index.zig b/std/meta/index.zig index 5bbbc1f9ae..c453e13729 100644 --- a/std/meta/index.zig +++ b/std/meta/index.zig @@ -21,7 +21,7 @@ pub fn tagName(v: var) []const u8 { unreachable; }, TypeId.Union => |info| { - const UnionTag = if(info.tag_type) |UT| UT else @compileError("union is untagged"); + const UnionTag = if (info.tag_type) |UT| UT else @compileError("union is untagged"); const Tag = @typeInfo(UnionTag).Enum.tag_type; inline for (info.fields) |field| { if (field.enum_field.?.value == @enumToInt(UnionTag(v))) @@ -37,8 +37,7 @@ pub fn tagName(v: var) []const u8 { unreachable; }, - else => @compileError("expected enum, error set or union type, found '" - ++ @typeName(T) ++ "'"), + else => @compileError("expected enum, error set or union type, found '" ++ @typeName(T) ++ "'"), } } @@ -92,7 +91,7 @@ test "std.meta.bitCount" { pub fn alignment(comptime T: type) u29 { //@alignOf works on non-pointer types - const P = if(comptime trait.is(TypeId.Pointer)(T)) T else *T; + const P = if (comptime trait.is(TypeId.Pointer)(T)) T else *T; return @typeInfo(P).Pointer.alignment; } @@ -109,9 +108,8 @@ pub fn Child(comptime T: type) type { TypeId.Array => |info| info.child, TypeId.Pointer => |info| info.child, TypeId.Optional => |info| info.child, - TypeId.Promise => |info| if(info.child) |child| child else null, - else => @compileError("Expected promise, pointer, optional, or array type, " - ++ "found '" ++ @typeName(T) ++ "'"), + TypeId.Promise => |info| if (info.child) |child| child else null, + else => @compileError("Expected promise, pointer, optional, or array type, " ++ "found '" ++ @typeName(T) ++ "'"), }; } @@ -128,8 +126,7 @@ pub fn containerLayout(comptime T: type) TypeInfo.ContainerLayout { TypeId.Struct => |info| info.layout, TypeId.Enum => |info| info.layout, TypeId.Union => |info| info.layout, - else => @compileError("Expected struct, enum or union type, found '" - ++ @typeName(T) ++ "'"), + else => @compileError("Expected struct, enum or union type, found '" ++ @typeName(T) ++ "'"), }; } @@ -172,8 +169,7 @@ pub fn definitions(comptime T: type) []TypeInfo.Definition { TypeId.Struct => |info| info.defs, TypeId.Enum => |info| info.defs, TypeId.Union => |info| info.defs, - else => @compileError("Expected struct, enum or union type, found '" - ++ @typeName(T) ++ "'"), + else => @compileError("Expected struct, enum or union type, found '" ++ @typeName(T) ++ "'"), }; } @@ -245,16 +241,14 @@ pub fn fields(comptime T: type) switch (@typeInfo(T)) { TypeId.Union => []TypeInfo.UnionField, TypeId.ErrorSet => []TypeInfo.Error, TypeId.Enum => []TypeInfo.EnumField, - else => @compileError("Expected struct, union, error set or enum type, found '" - ++ @typeName(T) ++ "'"), + else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"), } { return switch (@typeInfo(T)) { TypeId.Struct => |info| info.fields, TypeId.Union => |info| info.fields, TypeId.Enum => |info| info.fields, TypeId.ErrorSet => |info| info.errors, - else => @compileError("Expected struct, union, error set or enum type, found '" - ++ @typeName(T) ++ "'"), + else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"), }; } @@ -292,8 +286,7 @@ pub fn fieldInfo(comptime T: type, comptime field_name: []const u8) switch (@typ TypeId.Union => TypeInfo.UnionField, TypeId.ErrorSet => TypeInfo.Error, TypeId.Enum => TypeInfo.EnumField, - else => @compileError("Expected struct, union, error set or enum type, found '" - ++ @typeName(T) ++ "'"), + else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"), } { inline for (comptime fields(T)) |field| { if (comptime mem.eql(u8, field.name, field_name)) @@ -331,7 +324,7 @@ test "std.meta.fieldInfo" { pub fn TagType(comptime T: type) type { return switch (@typeInfo(T)) { TypeId.Enum => |info| info.tag_type, - TypeId.Union => |info| if(info.tag_type) |Tag| Tag else null, + TypeId.Union => |info| if (info.tag_type) |Tag| Tag else null, else => @compileError("expected enum or union type, found '" ++ @typeName(T) ++ "'"), }; } @@ -350,104 +343,80 @@ test "std.meta.TagType" { debug.assert(TagType(U) == E); } - - ///Returns the active tag of a tagged union -pub fn activeTag(u: var) @TagType(@typeOf(u)) -{ +pub fn activeTag(u: var) @TagType(@typeOf(u)) { const T = @typeOf(u); return @TagType(T)(u); } -test "std.meta.activeTag" -{ - const UE = enum. - { +test "std.meta.activeTag" { + const UE = enum.{ Int, Float, }; - - const U = union(UE). - { + + const U = union(UE).{ Int: u32, Float: f32, }; - - var u = U.{ .Int = 32, }; - debug.assert(activeTag(u) == UE.Int); - - u = U.{ .Float = 112.9876, }; - debug.assert(activeTag(u) == UE.Float); + var u = U.{ .Int = 32 }; + debug.assert(activeTag(u) == UE.Int); + + u = U.{ .Float = 112.9876 }; + debug.assert(activeTag(u) == UE.Float); } ///Compares two of any type for equality. Containers are compared on a field-by-field basis, /// where possible. Pointers are not followed. -pub fn eql(a: var, b: @typeOf(a)) bool -{ +pub fn eql(a: var, b: @typeOf(a)) bool { const T = @typeOf(a); - - switch(@typeId(T)) - { - builtin.TypeId.Struct => - { + + switch (@typeId(T)) { + builtin.TypeId.Struct => { const info = @typeInfo(T).Struct; - - inline for(info.fields) |field_info| - { - if(!eql(@field(a, field_info.name), - @field(b, field_info.name))) return false; + + inline for (info.fields) |field_info| { + if (!eql(@field(a, field_info.name), @field(b, field_info.name))) return false; } return true; }, - builtin.TypeId.ErrorUnion => - { - 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; + builtin.TypeId.ErrorUnion => { + 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; } }, - builtin.TypeId.Union => - { + builtin.TypeId.Union => { const info = @typeInfo(T).Union; - - if(info.tag_type) |_| - { + + if (info.tag_type) |_| { const tag_a = activeTag(a); const tag_b = activeTag(b); - if(tag_a != tag_b) return false; - - inline for(info.fields) |field_info| - { + if (tag_a != tag_b) return false; + + inline for (info.fields) |field_info| { const enum_field = field_info.enum_field.?; - if(enum_field.value == @enumToInt(tag_a)) - { - return eql(@field(a, enum_field.name), - @field(b, enum_field.name)); + if (enum_field.value == @enumToInt(tag_a)) { + return eql(@field(a, enum_field.name), @field(b, enum_field.name)); } } return false; } - + @compileError("cannot compare untagged union type " ++ @typeName(T)); }, - builtin.TypeId.Array => - { - if(a.len != b.len) return false; - for(a) |e, i| if(!eql(e, b[i])) return false; + builtin.TypeId.Array => { + if (a.len != b.len) return false; + for (a) |e, i| + if (!eql(e, b[i])) return false; return true; }, - builtin.TypeId.Pointer => - { + builtin.TypeId.Pointer => { const info = @typeInfo(T).Pointer; - switch(info.size) - { - builtin.TypeInfo.Pointer.Size.One, - builtin.TypeInfo.Pointer.Size.Many => return a == b, + switch (info.size) { + builtin.TypeInfo.Pointer.Size.One, builtin.TypeInfo.Pointer.Size.Many => return a == b, builtin.TypeInfo.Pointer.Size.Slice => return a.ptr == b.ptr and a.len == b.len, } }, @@ -455,71 +424,62 @@ pub fn eql(a: var, b: @typeOf(a)) bool } } - -test "std.meta.eql" -{ - const S = struct. - { +test "std.meta.eql" { + const S = struct.{ a: u32, b: f64, c: [5]u8, }; - - const U = union(enum). - { + + const U = union(enum).{ s: S, f: f32, }; - - const s_1 = S. - { + + const s_1 = S.{ .a = 134, .b = 123.3, .c = "12345", }; - - const s_2 = S. - { + + const s_2 = S.{ .a = 1, .b = 123.3, .c = "54321", }; - - const s_3 = S. - { + + const s_3 = S.{ .a = 134, .b = 123.3, .c = "12345", }; - - const u_1 = U.{ .f = 24, }; - const u_2 = U.{ .s = s_1, }; - const u_3 = U.{ .f = 24, }; - + + const u_1 = U.{ .f = 24 }; + const u_2 = U.{ .s = s_1 }; + const u_3 = U.{ .f = 24 }; + debug.assert(eql(s_1, s_3)); debug.assert(eql(&s_1, &s_1)); debug.assert(!eql(&s_1, &s_3)); debug.assert(eql(u_1, u_3)); debug.assert(!eql(u_1, u_2)); - + var a1 = "abcdef"; var a2 = "abcdef"; var a3 = "ghijkl"; - + debug.assert(eql(a1, a2)); debug.assert(!eql(a1, a3)); debug.assert(!eql(a1[0..], a2[0..])); - - const EU = struct. - { - fn tst(err: bool) !u8 - { - if(err) return error.Error; + + const EU = struct.{ + fn tst(err: bool) !u8 { + if (err) return error.Error; return u8(5); } }; - + debug.assert(eql(EU.tst(true), EU.tst(true))); debug.assert(eql(EU.tst(false), EU.tst(false))); debug.assert(!eql(EU.tst(false), EU.tst(true))); -} \ No newline at end of file +} diff --git a/std/meta/trait.zig b/std/meta/trait.zig index 4f5ab801d7..21f71afc4e 100644 --- a/std/meta/trait.zig +++ b/std/meta/trait.zig @@ -8,101 +8,80 @@ const meta = @import("index.zig"); //This is necessary if we want to return generic functions directly because of how the // the type erasure works. see: #1375 -fn traitFnWorkaround(comptime T: type) bool -{ +fn traitFnWorkaround(comptime T: type) bool { return false; } pub const TraitFn = @typeOf(traitFnWorkaround); - /// //////Trait generators //Need TraitList because compiler can't do varargs at comptime yet pub const TraitList = []const TraitFn; -pub fn multiTrait(comptime traits: TraitList) TraitFn -{ - const Closure = struct. - { - pub fn trait(comptime T: type) bool - { - inline for(traits) |t| if(!t(T)) return false; +pub fn multiTrait(comptime traits: TraitList) TraitFn { + const Closure = struct.{ + pub fn trait(comptime T: type) bool { + inline for (traits) |t| + if (!t(T)) return false; return true; } }; return Closure.trait; } -test "std.meta.trait.multiTrait" -{ - const Vector2 = struct. - { +test "std.meta.trait.multiTrait" { + const Vector2 = struct.{ const MyType = @This(); - + x: u8, y: u8, - - pub fn add(self: MyType, other: MyType) MyType - { - return MyType. - { + + pub fn add(self: MyType, other: MyType) MyType { + return MyType.{ .x = self.x + other.x, .y = self.y + other.y, }; } }; - - const isVector = multiTrait - ( - TraitList. - { - hasFn("add"), - hasField("x"), - hasField("y"), - } - ); + + const isVector = multiTrait(TraitList.{ + hasFn("add"), + hasField("x"), + hasField("y"), + }); debug.assert(isVector(Vector2)); debug.assert(!isVector(u8)); } /// - -pub fn hasDef(comptime name: []const u8) TraitFn -{ - const Closure = struct. - { - pub fn trait(comptime T: type) bool - { +pub fn hasDef(comptime name: []const u8) TraitFn { + const Closure = struct.{ + pub fn trait(comptime T: type) bool { const info = @typeInfo(T); - const defs = switch(info) - { + const defs = switch (info) { builtin.TypeId.Struct => |s| s.defs, builtin.TypeId.Union => |u| u.defs, builtin.TypeId.Enum => |e| e.defs, else => return false, }; - inline for(defs) |def| - { - if(mem.eql(u8, def.name, name)) return def.is_pub; + inline for (defs) |def| { + if (mem.eql(u8, def.name, name)) return def.is_pub; } - + return false; } }; return Closure.trait; } -test "std.meta.trait.hasDef" -{ - const TestStruct = struct. - { +test "std.meta.trait.hasDef" { + const TestStruct = struct.{ pub const value = u8(16); }; - - const TestStructFail = struct. - { + + const TestStructFail = struct.{ const value = u8(16); }; @@ -115,13 +94,10 @@ test "std.meta.trait.hasDef" } /// -pub fn hasFn(comptime name: []const u8) TraitFn -{ - const Closure = struct. - { - pub fn trait(comptime T: type) bool - { - if(!comptime hasDef(name)(T)) return false; +pub fn hasFn(comptime name: []const u8) TraitFn { + const Closure = struct.{ + pub fn trait(comptime T: type) bool { + if (!comptime hasDef(name)(T)) return false; const DefType = @typeOf(@field(T, name)); const def_type_id = @typeId(DefType); return def_type_id == builtin.TypeId.Fn; @@ -130,10 +106,8 @@ pub fn hasFn(comptime name: []const u8) TraitFn return Closure.trait; } -test "std.meta.trait.hasFn" -{ - const TestStruct = struct. - { +test "std.meta.trait.hasFn" { + const TestStruct = struct.{ pub fn useless() void {} }; @@ -143,36 +117,29 @@ test "std.meta.trait.hasFn" } /// -pub fn hasField(comptime name: []const u8) TraitFn -{ - const Closure = struct. - { - pub fn trait(comptime T: type) bool - { +pub fn hasField(comptime name: []const u8) TraitFn { + const Closure = struct.{ + pub fn trait(comptime T: type) bool { const info = @typeInfo(T); - const fields = switch(info) - { + const fields = switch (info) { builtin.TypeId.Struct => |s| s.fields, builtin.TypeId.Union => |u| u.fields, builtin.TypeId.Enum => |e| e.fields, else => return false, }; - inline for(fields) |field| - { - if(mem.eql(u8, field.name, name)) return true; + inline for (fields) |field| { + if (mem.eql(u8, field.name, name)) return true; } - + return false; } }; return Closure.trait; } -test "std.meta.trait.hasField" -{ - const TestStruct = struct. - { +test "std.meta.trait.hasField" { + const TestStruct = struct.{ value: u32, }; @@ -184,21 +151,16 @@ test "std.meta.trait.hasField" } /// - -pub fn is(comptime id: builtin.TypeId) TraitFn -{ - const Closure = struct. - { - pub fn trait(comptime T: type) bool - { +pub fn is(comptime id: builtin.TypeId) TraitFn { + const Closure = struct.{ + pub fn trait(comptime T: type) bool { return id == @typeId(T); } }; return Closure.trait; } -test "std.meta.trait.is" -{ +test "std.meta.trait.is" { debug.assert(is(builtin.TypeId.Int)(u8)); debug.assert(!is(builtin.TypeId.Int)(f32)); debug.assert(is(builtin.TypeId.Pointer)(*u8)); @@ -207,39 +169,31 @@ test "std.meta.trait.is" } /// - -pub fn isPtrTo(comptime id: builtin.TypeId) TraitFn -{ - const Closure = struct. - { - pub fn trait(comptime T: type) bool - { - if(!comptime isSingleItemPtr(T)) return false; +pub fn isPtrTo(comptime id: builtin.TypeId) TraitFn { + const Closure = struct.{ + pub fn trait(comptime T: type) bool { + if (!comptime isSingleItemPtr(T)) return false; return id == @typeId(meta.Child(T)); } }; return Closure.trait; } -test "std.meta.trait.isPtrTo" -{ +test "std.meta.trait.isPtrTo" { debug.assert(!isPtrTo(builtin.TypeId.Struct)(struct.{})); debug.assert(isPtrTo(builtin.TypeId.Struct)(*struct.{})); debug.assert(!isPtrTo(builtin.TypeId.Struct)(**struct.{})); } - ///////////Strait trait Fns //@TODO: // Somewhat limited since we can't apply this logic to normal variables, fields, or // Fns yet. Should be isExternType? -pub fn isExtern(comptime T: type) bool -{ +pub fn isExtern(comptime T: type) bool { const Extern = builtin.TypeInfo.ContainerLayout.Extern; const info = @typeInfo(T); - return switch(info) - { + return switch (info) { builtin.TypeId.Struct => |s| s.layout == Extern, builtin.TypeId.Union => |u| u.layout == Extern, builtin.TypeId.Enum => |e| e.layout == Extern, @@ -247,8 +201,7 @@ pub fn isExtern(comptime T: type) bool }; } -test "std.meta.trait.isExtern" -{ +test "std.meta.trait.isExtern" { const TestExStruct = extern struct.{}; const TestStruct = struct.{}; @@ -258,13 +211,10 @@ test "std.meta.trait.isExtern" } /// - -pub fn isPacked(comptime T: type) bool -{ +pub fn isPacked(comptime T: type) bool { const Packed = builtin.TypeInfo.ContainerLayout.Packed; const info = @typeInfo(T); - return switch(info) - { + return switch (info) { builtin.TypeId.Struct => |s| s.layout == Packed, builtin.TypeId.Union => |u| u.layout == Packed, builtin.TypeId.Enum => |e| e.layout == Packed, @@ -272,8 +222,7 @@ pub fn isPacked(comptime T: type) bool }; } -test "std.meta.trait.isPacked" -{ +test "std.meta.trait.isPacked" { const TestPStruct = packed struct.{}; const TestStruct = struct.{}; @@ -283,19 +232,15 @@ test "std.meta.trait.isPacked" } /// - -pub fn isSingleItemPtr(comptime T: type) bool -{ - if(comptime is(builtin.TypeId.Pointer)(T)) - { +pub fn isSingleItemPtr(comptime T: type) bool { + if (comptime is(builtin.TypeId.Pointer)(T)) { const info = @typeInfo(T); return info.Pointer.size == builtin.TypeInfo.Pointer.Size.One; } return false; } -test "std.meta.trait.isSingleItemPtr" -{ +test "std.meta.trait.isSingleItemPtr" { const array = []u8.{0} ** 10; debug.assert(isSingleItemPtr(@typeOf(&array[0]))); debug.assert(!isSingleItemPtr(@typeOf(array))); @@ -303,19 +248,15 @@ test "std.meta.trait.isSingleItemPtr" } /// - -pub fn isManyItemPtr(comptime T: type) bool -{ - if(comptime is(builtin.TypeId.Pointer)(T)) - { +pub fn isManyItemPtr(comptime T: type) bool { + if (comptime is(builtin.TypeId.Pointer)(T)) { const info = @typeInfo(T); return info.Pointer.size == builtin.TypeInfo.Pointer.Size.Many; } return false; } -test "std.meta.trait.isManyItemPtr" -{ +test "std.meta.trait.isManyItemPtr" { const array = []u8.{0} ** 10; const mip = @ptrCast([*]const u8, &array[0]); debug.assert(isManyItemPtr(@typeOf(mip))); @@ -324,19 +265,15 @@ test "std.meta.trait.isManyItemPtr" } /// - -pub fn isSlice(comptime T: type) bool -{ - if(comptime is(builtin.TypeId.Pointer)(T)) - { +pub fn isSlice(comptime T: type) bool { + if (comptime is(builtin.TypeId.Pointer)(T)) { const info = @typeInfo(T); return info.Pointer.size == builtin.TypeInfo.Pointer.Size.Slice; } return false; } -test "std.meta.trait.isSlice" -{ +test "std.meta.trait.isSlice" { const array = []u8.{0} ** 10; debug.assert(isSlice(@typeOf(array[0..]))); debug.assert(!isSlice(@typeOf(array))); @@ -344,27 +281,22 @@ test "std.meta.trait.isSlice" } /// - -pub fn isIndexable(comptime T: type) bool -{ - if(comptime is(builtin.TypeId.Pointer)(T)) - { - const info = @typeInfo(T); - if(info.Pointer.size == builtin.TypeInfo.Pointer.Size.One) - { - if(comptime is(builtin.TypeId.Array)(meta.Child(T))) return true; - return false; - } - return true; - } - return comptime is(builtin.TypeId.Array)(T); +pub fn isIndexable(comptime T: type) bool { + if (comptime is(builtin.TypeId.Pointer)(T)) { + const info = @typeInfo(T); + if (info.Pointer.size == builtin.TypeInfo.Pointer.Size.One) { + if (comptime is(builtin.TypeId.Array)(meta.Child(T))) return true; + return false; + } + return true; + } + return comptime is(builtin.TypeId.Array)(T); } -test "std.meta.trait.isIndexable" -{ +test "std.meta.trait.isIndexable" { const array = []u8.{0} ** 10; const slice = array[0..]; - + debug.assert(isIndexable(@typeOf(array))); debug.assert(isIndexable(@typeOf(&array))); debug.assert(isIndexable(@typeOf(slice))); @@ -372,26 +304,18 @@ test "std.meta.trait.isIndexable" } /// - -pub fn isNumber(comptime T: type) bool -{ - return switch(@typeId(T)) - { - builtin.TypeId.Int, - builtin.TypeId.Float, - builtin.TypeId.ComptimeInt, - builtin.TypeId.ComptimeFloat => true, +pub fn isNumber(comptime T: type) bool { + return switch (@typeId(T)) { + builtin.TypeId.Int, builtin.TypeId.Float, builtin.TypeId.ComptimeInt, builtin.TypeId.ComptimeFloat => true, else => false, }; } -test "std.meta.trait.isNumber" -{ - const NotANumber = struct. - { +test "std.meta.trait.isNumber" { + const NotANumber = struct.{ number: u8, }; - + debug.assert(isNumber(u32)); debug.assert(isNumber(f32)); debug.assert(isNumber(u64)); @@ -402,16 +326,13 @@ test "std.meta.trait.isNumber" } /// - -pub fn isConstPtr(comptime T: type) bool -{ - if(!comptime is(builtin.TypeId.Pointer)(T)) return false; +pub fn isConstPtr(comptime T: type) bool { + if (!comptime is(builtin.TypeId.Pointer)(T)) return false; const info = @typeInfo(T); return info.Pointer.is_const; } -test "std.meta.trait.isConstPtr" -{ +test "std.meta.trait.isConstPtr" { var t = u8(0); const c = u8(0); debug.assert(isConstPtr(*const @typeOf(t))); @@ -421,12 +342,9 @@ test "std.meta.trait.isConstPtr" } /// - -pub fn isContainer(comptime T: type) bool -{ +pub fn isContainer(comptime T: type) bool { const info = @typeInfo(T); - return switch(info) - { + return switch (info) { builtin.TypeId.Struct => true, builtin.TypeId.Union => true, builtin.TypeId.Enum => true, @@ -434,16 +352,18 @@ pub fn isContainer(comptime T: type) bool }; } -test "std.meta.trait.isContainer" -{ +test "std.meta.trait.isContainer" { const TestStruct = struct.{}; - const TestUnion = union.{ a: void, }; - const TestEnum = enum.{ A, B, }; - + const TestUnion = union.{ + a: void, + }; + const TestEnum = enum.{ + A, + B, + }; + debug.assert(isContainer(TestStruct)); debug.assert(isContainer(TestUnion)); debug.assert(isContainer(TestEnum)); debug.assert(!isContainer(u8)); } - -/// \ No newline at end of file