| author | |
| committer | |
| log | db3b768eabaf0ca7f83cccabfaa61eb0aa400c41 |
| tree | 7cff9bfe7a108346e749a52920d9e69fe8320c9e |
| parent | 2a3fdd52ce7bd0cc577947e49bc8a3e69a34b6d0 |
3 files changed, 232 insertions(+), 364 deletions(-)
std/mem.zig+61-73| ... | ... | @@ -866,128 +866,123 @@ test "std.mem.endianSwap" { |
| 866 | 866 | assert(endianSwap(u32, 0xDEADBEEF) == 0xEFBEADDE); |
| 867 | 867 | } |
| 868 | 868 | |
| 869 | ||
| 870 | ||
| 871 | fn AsBytesReturnType(comptime P: type) type | |
| 872 | { | |
| 873 | if(comptime !trait.isSingleItemPtr(P)) @compileError("expected single item " | |
| 874 | ++ "pointer, passed " ++ @typeName(P)); | |
| 869 | fn AsBytesReturnType(comptime P: type) type { | |
| 870 | if (comptime !trait.isSingleItemPtr(P)) | |
| 871 | @compileError("expected single item " ++ "pointer, passed " ++ @typeName(P)); | |
| 875 | 872 | |
| 876 | 873 | const size = usize(@sizeOf(meta.Child(P))); |
| 877 | 874 | const alignment = comptime meta.alignment(P); |
| 878 | if(comptime trait.isConstPtr(P)) return *align(alignment) const [size]u8; | |
| 875 | ||
| 876 | if (comptime trait.isConstPtr(P)) | |
| 877 | return *align(alignment) const [size]u8; | |
| 879 | 878 | return *align(alignment) [size]u8; |
| 880 | 879 | } |
| 881 | 880 | |
| 882 | 881 | ///Given a pointer to a single item, returns a slice of the underlying bytes, preserving constness. |
| 883 | pub fn asBytes(ptr: var) AsBytesReturnType(@typeOf(ptr)) | |
| 884 | { | |
| 882 | pub fn asBytes(ptr: var) AsBytesReturnType(@typeOf(ptr)) { | |
| 885 | 883 | const P = @typeOf(ptr); |
| 886 | 884 | return @ptrCast(AsBytesReturnType(P), ptr); |
| 887 | 885 | } |
| 888 | 886 | |
| 889 | test "std.mem.asBytes" | |
| 890 | { | |
| 887 | test "std.mem.asBytes" { | |
| 891 | 888 | const deadbeef = u32(0xDEADBEEF); |
| 892 | const deadbeef_bytes = switch(builtin.endian) | |
| 893 | { | |
| 889 | const deadbeef_bytes = switch (builtin.endian) { | |
| 894 | 890 | builtin.Endian.Big => "\xDE\xAD\xBE\xEF", |
| 895 | 891 | builtin.Endian.Little => "\xEF\xBE\xAD\xDE", |
| 896 | 892 | }; |
| 897 | ||
| 893 | ||
| 898 | 894 | debug.assert(std.mem.eql(u8, asBytes(&deadbeef), deadbeef_bytes)); |
| 899 | ||
| 895 | ||
| 900 | 896 | var codeface = u32(0xC0DEFACE); |
| 901 | for(asBytes(&codeface).*) |*b| b.* = 0; | |
| 897 | for (asBytes(&codeface).*) |*b| | |
| 898 | b.* = 0; | |
| 902 | 899 | debug.assert(codeface == 0); |
| 903 | ||
| 904 | const S = packed struct. | |
| 905 | { | |
| 900 | ||
| 901 | const S = packed struct.{ | |
| 906 | 902 | a: u8, |
| 907 | 903 | b: u8, |
| 908 | 904 | c: u8, |
| 909 | 905 | d: u8, |
| 910 | 906 | }; |
| 911 | ||
| 912 | const inst = S.{ .a = 0xBE, .b = 0xEF, .c = 0xDE, .d = 0xA1, }; | |
| 907 | ||
| 908 | const inst = S.{ | |
| 909 | .a = 0xBE, | |
| 910 | .b = 0xEF, | |
| 911 | .c = 0xDE, | |
| 912 | .d = 0xA1, | |
| 913 | }; | |
| 913 | 914 | debug.assert(std.mem.eql(u8, asBytes(&inst), "\xBE\xEF\xDE\xA1")); |
| 914 | 915 | } |
| 915 | 916 | |
| 916 | 917 | ///Given any value, returns a copy of its bytes in an array. |
| 917 | pub fn toBytes(value: var) [@sizeOf(@typeOf(value))]u8 | |
| 918 | { | |
| 918 | pub fn toBytes(value: var) [@sizeOf(@typeOf(value))]u8 { | |
| 919 | 919 | return asBytes(&value).*; |
| 920 | 920 | } |
| 921 | 921 | |
| 922 | test "std.mem.toBytes" | |
| 923 | { | |
| 922 | test "std.mem.toBytes" { | |
| 924 | 923 | var my_bytes = toBytes(u32(0x12345678)); |
| 925 | switch(builtin.endian) | |
| 926 | { | |
| 924 | switch (builtin.endian) { | |
| 927 | 925 | builtin.Endian.Big => debug.assert(std.mem.eql(u8, my_bytes, "\x12\x34\x56\x78")), |
| 928 | 926 | builtin.Endian.Little => debug.assert(std.mem.eql(u8, my_bytes, "\x78\x56\x34\x12")), |
| 929 | 927 | } |
| 930 | ||
| 928 | ||
| 931 | 929 | my_bytes[0] = '\x99'; |
| 932 | switch(builtin.endian) | |
| 933 | { | |
| 930 | switch (builtin.endian) { | |
| 934 | 931 | builtin.Endian.Big => debug.assert(std.mem.eql(u8, my_bytes, "\x99\x34\x56\x78")), |
| 935 | 932 | builtin.Endian.Little => debug.assert(std.mem.eql(u8, my_bytes, "\x99\x56\x34\x12")), |
| 936 | 933 | } |
| 937 | 934 | } |
| 938 | 935 | |
| 939 | ||
| 940 | fn BytesAsValueReturnType(comptime T: type, comptime B: type) type | |
| 941 | { | |
| 936 | fn BytesAsValueReturnType(comptime T: type, comptime B: type) type { | |
| 942 | 937 | const size = usize(@sizeOf(T)); |
| 943 | 938 | |
| 944 | if(comptime !trait.is(builtin.TypeId.Pointer)(B) or meta.Child(B) != [size]u8) | |
| 945 | { | |
| 939 | if (comptime !trait.is(builtin.TypeId.Pointer)(B) or meta.Child(B) != [size]u8) { | |
| 946 | 940 | @compileError("expected *[N]u8 " ++ ", passed " ++ @typeName(B)); |
| 947 | 941 | } |
| 948 | ||
| 942 | ||
| 949 | 943 | const alignment = comptime meta.alignment(B); |
| 950 | ||
| 951 | return if(comptime trait.isConstPtr(B)) *align(alignment) const T else *align(alignment) T; | |
| 944 | ||
| 945 | return if (comptime trait.isConstPtr(B)) *align(alignment) const T else *align(alignment) T; | |
| 952 | 946 | } |
| 953 | 947 | |
| 954 | 948 | ///Given a pointer to an array of bytes, returns a pointer to a value of the specified type |
| 955 | 949 | /// backed by those bytes, preserving constness. |
| 956 | pub fn bytesAsValue(comptime T: type, bytes: var) BytesAsValueReturnType(T, @typeOf(bytes)) | |
| 957 | { | |
| 950 | pub fn bytesAsValue(comptime T: type, bytes: var) BytesAsValueReturnType(T, @typeOf(bytes)) { | |
| 958 | 951 | return @ptrCast(BytesAsValueReturnType(T, @typeOf(bytes)), bytes); |
| 959 | 952 | } |
| 960 | 953 | |
| 961 | test "std.mem.bytesAsValue" | |
| 962 | { | |
| 954 | test "std.mem.bytesAsValue" { | |
| 963 | 955 | const deadbeef = u32(0xDEADBEEF); |
| 964 | const deadbeef_bytes = switch(builtin.endian) | |
| 965 | { | |
| 956 | const deadbeef_bytes = switch (builtin.endian) { | |
| 966 | 957 | builtin.Endian.Big => "\xDE\xAD\xBE\xEF", |
| 967 | 958 | builtin.Endian.Little => "\xEF\xBE\xAD\xDE", |
| 968 | 959 | }; |
| 969 | ||
| 960 | ||
| 970 | 961 | debug.assert(deadbeef == bytesAsValue(u32, &deadbeef_bytes).*); |
| 971 | ||
| 972 | var codeface_bytes = switch(builtin.endian) | |
| 973 | { | |
| 962 | ||
| 963 | var codeface_bytes = switch (builtin.endian) { | |
| 974 | 964 | builtin.Endian.Big => "\xC0\xDE\xFA\xCE", |
| 975 | 965 | builtin.Endian.Little => "\xCE\xFA\xDE\xC0", |
| 976 | 966 | }; |
| 977 | 967 | var codeface = bytesAsValue(u32, &codeface_bytes); |
| 978 | 968 | debug.assert(codeface.* == 0xC0DEFACE); |
| 979 | 969 | codeface.* = 0; |
| 980 | for(codeface_bytes) |b| debug.assert(b == 0); | |
| 981 | ||
| 982 | const S = packed struct. | |
| 983 | { | |
| 970 | for (codeface_bytes) |b| | |
| 971 | debug.assert(b == 0); | |
| 972 | ||
| 973 | const S = packed struct.{ | |
| 984 | 974 | a: u8, |
| 985 | 975 | b: u8, |
| 986 | 976 | c: u8, |
| 987 | 977 | d: u8, |
| 988 | 978 | }; |
| 989 | ||
| 990 | const inst = S.{ .a = 0xBE, .b = 0xEF, .c = 0xDE, .d = 0xA1, }; | |
| 979 | ||
| 980 | const inst = S.{ | |
| 981 | .a = 0xBE, | |
| 982 | .b = 0xEF, | |
| 983 | .c = 0xDE, | |
| 984 | .d = 0xA1, | |
| 985 | }; | |
| 991 | 986 | const inst_bytes = "\xBE\xEF\xDE\xA1"; |
| 992 | 987 | const inst2 = bytesAsValue(S, &inst_bytes); |
| 993 | 988 | debug.assert(meta.eql(inst, inst2.*)); |
| ... | ... | @@ -995,50 +990,43 @@ test "std.mem.bytesAsValue" |
| 995 | 990 | |
| 996 | 991 | ///Given a pointer to an array of bytes, returns a value of the specified type backed by a |
| 997 | 992 | /// copy of those bytes. |
| 998 | pub fn bytesToValue(comptime T: type, bytes: var) T | |
| 999 | { | |
| 993 | pub fn bytesToValue(comptime T: type, bytes: var) T { | |
| 1000 | 994 | return bytesAsValue(T, &bytes).*; |
| 1001 | 995 | } |
| 1002 | test "std.mem.bytesToValue" | |
| 1003 | { | |
| 1004 | const deadbeef_bytes = switch(builtin.endian) | |
| 1005 | { | |
| 996 | test "std.mem.bytesToValue" { | |
| 997 | const deadbeef_bytes = switch (builtin.endian) { | |
| 1006 | 998 | builtin.Endian.Big => "\xDE\xAD\xBE\xEF", |
| 1007 | 999 | builtin.Endian.Little => "\xEF\xBE\xAD\xDE", |
| 1008 | 1000 | }; |
| 1009 | ||
| 1001 | ||
| 1010 | 1002 | const deadbeef = bytesToValue(u32, deadbeef_bytes); |
| 1011 | 1003 | debug.assert(deadbeef == u32(0xDEADBEEF)); |
| 1012 | 1004 | } |
| 1013 | 1005 | |
| 1014 | ||
| 1015 | fn SubArrayPtrReturnType(comptime T: type, comptime length: usize) type | |
| 1016 | { | |
| 1017 | if(trait.isConstPtr(T)) return *const [length]meta.Child(meta.Child(T)); | |
| 1006 | fn SubArrayPtrReturnType(comptime T: type, comptime length: usize) type { | |
| 1007 | if (trait.isConstPtr(T)) | |
| 1008 | return *const [length]meta.Child(meta.Child(T)); | |
| 1018 | 1009 | return *[length]meta.Child(meta.Child(T)); |
| 1019 | 1010 | } |
| 1020 | 1011 | |
| 1021 | 1012 | ///Given a pointer to an array, returns a pointer to a portion of that array, preserving constness. |
| 1022 | pub fn subArrayPtr(ptr: var, comptime start: usize, comptime length: usize) | |
| 1023 | SubArrayPtrReturnType(@typeOf(ptr), length) | |
| 1024 | { | |
| 1013 | pub fn subArrayPtr(ptr: var, comptime start: usize, comptime length: usize) SubArrayPtrReturnType(@typeOf(ptr), length) { | |
| 1025 | 1014 | debug.assert(start + length <= ptr.*.len); |
| 1026 | ||
| 1015 | ||
| 1027 | 1016 | const ReturnType = SubArrayPtrReturnType(@typeOf(ptr), length); |
| 1028 | 1017 | const T = meta.Child(meta.Child(@typeOf(ptr))); |
| 1029 | 1018 | return @ptrCast(ReturnType, &ptr[start]); |
| 1030 | 1019 | } |
| 1031 | 1020 | |
| 1032 | test "std.mem.subArrayPtr" | |
| 1033 | { | |
| 1021 | test "std.mem.subArrayPtr" { | |
| 1034 | 1022 | const a1 = "abcdef"; |
| 1035 | 1023 | const sub1 = subArrayPtr(&a1, 2, 3); |
| 1036 | 1024 | debug.assert(std.mem.eql(u8, sub1.*, "cde")); |
| 1037 | ||
| 1025 | ||
| 1038 | 1026 | var a2 = "abcdef"; |
| 1039 | 1027 | var sub2 = subArrayPtr(&a2, 2, 3); |
| 1040 | 1028 | |
| 1041 | 1029 | debug.assert(std.mem.eql(u8, sub2, "cde")); |
| 1042 | 1030 | sub2[1] = 'X'; |
| 1043 | 1031 | debug.assert(std.mem.eql(u8, a2, "abcXef")); |
| 1044 | } | |
| \ No newline at end of file | ||
| 1032 | } |
std/meta/index.zig+71-111| ... | ... | @@ -21,7 +21,7 @@ pub fn tagName(v: var) []const u8 { |
| 21 | 21 | unreachable; |
| 22 | 22 | }, |
| 23 | 23 | TypeId.Union => |info| { |
| 24 | const UnionTag = if(info.tag_type) |UT| UT else @compileError("union is untagged"); | |
| 24 | const UnionTag = if (info.tag_type) |UT| UT else @compileError("union is untagged"); | |
| 25 | 25 | const Tag = @typeInfo(UnionTag).Enum.tag_type; |
| 26 | 26 | inline for (info.fields) |field| { |
| 27 | 27 | if (field.enum_field.?.value == @enumToInt(UnionTag(v))) |
| ... | ... | @@ -37,8 +37,7 @@ pub fn tagName(v: var) []const u8 { |
| 37 | 37 | |
| 38 | 38 | unreachable; |
| 39 | 39 | }, |
| 40 | else => @compileError("expected enum, error set or union type, found '" | |
| 41 | ++ @typeName(T) ++ "'"), | |
| 40 | else => @compileError("expected enum, error set or union type, found '" ++ @typeName(T) ++ "'"), | |
| 42 | 41 | } |
| 43 | 42 | } |
| 44 | 43 | |
| ... | ... | @@ -92,7 +91,7 @@ test "std.meta.bitCount" { |
| 92 | 91 | |
| 93 | 92 | pub fn alignment(comptime T: type) u29 { |
| 94 | 93 | //@alignOf works on non-pointer types |
| 95 | const P = if(comptime trait.is(TypeId.Pointer)(T)) T else *T; | |
| 94 | const P = if (comptime trait.is(TypeId.Pointer)(T)) T else *T; | |
| 96 | 95 | return @typeInfo(P).Pointer.alignment; |
| 97 | 96 | } |
| 98 | 97 | |
| ... | ... | @@ -109,9 +108,8 @@ pub fn Child(comptime T: type) type { |
| 109 | 108 | TypeId.Array => |info| info.child, |
| 110 | 109 | TypeId.Pointer => |info| info.child, |
| 111 | 110 | TypeId.Optional => |info| info.child, |
| 112 | TypeId.Promise => |info| if(info.child) |child| child else null, | |
| 113 | else => @compileError("Expected promise, pointer, optional, or array type, " | |
| 114 | ++ "found '" ++ @typeName(T) ++ "'"), | |
| 111 | TypeId.Promise => |info| if (info.child) |child| child else null, | |
| 112 | else => @compileError("Expected promise, pointer, optional, or array type, " ++ "found '" ++ @typeName(T) ++ "'"), | |
| 115 | 113 | }; |
| 116 | 114 | } |
| 117 | 115 | |
| ... | ... | @@ -128,8 +126,7 @@ pub fn containerLayout(comptime T: type) TypeInfo.ContainerLayout { |
| 128 | 126 | TypeId.Struct => |info| info.layout, |
| 129 | 127 | TypeId.Enum => |info| info.layout, |
| 130 | 128 | TypeId.Union => |info| info.layout, |
| 131 | else => @compileError("Expected struct, enum or union type, found '" | |
| 132 | ++ @typeName(T) ++ "'"), | |
| 129 | else => @compileError("Expected struct, enum or union type, found '" ++ @typeName(T) ++ "'"), | |
| 133 | 130 | }; |
| 134 | 131 | } |
| 135 | 132 | |
| ... | ... | @@ -172,8 +169,7 @@ pub fn definitions(comptime T: type) []TypeInfo.Definition { |
| 172 | 169 | TypeId.Struct => |info| info.defs, |
| 173 | 170 | TypeId.Enum => |info| info.defs, |
| 174 | 171 | TypeId.Union => |info| info.defs, |
| 175 | else => @compileError("Expected struct, enum or union type, found '" | |
| 176 | ++ @typeName(T) ++ "'"), | |
| 172 | else => @compileError("Expected struct, enum or union type, found '" ++ @typeName(T) ++ "'"), | |
| 177 | 173 | }; |
| 178 | 174 | } |
| 179 | 175 | |
| ... | ... | @@ -245,16 +241,14 @@ pub fn fields(comptime T: type) switch (@typeInfo(T)) { |
| 245 | 241 | TypeId.Union => []TypeInfo.UnionField, |
| 246 | 242 | TypeId.ErrorSet => []TypeInfo.Error, |
| 247 | 243 | TypeId.Enum => []TypeInfo.EnumField, |
| 248 | else => @compileError("Expected struct, union, error set or enum type, found '" | |
| 249 | ++ @typeName(T) ++ "'"), | |
| 244 | else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"), | |
| 250 | 245 | } { |
| 251 | 246 | return switch (@typeInfo(T)) { |
| 252 | 247 | TypeId.Struct => |info| info.fields, |
| 253 | 248 | TypeId.Union => |info| info.fields, |
| 254 | 249 | TypeId.Enum => |info| info.fields, |
| 255 | 250 | TypeId.ErrorSet => |info| info.errors, |
| 256 | else => @compileError("Expected struct, union, error set or enum type, found '" | |
| 257 | ++ @typeName(T) ++ "'"), | |
| 251 | else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"), | |
| 258 | 252 | }; |
| 259 | 253 | } |
| 260 | 254 | |
| ... | ... | @@ -292,8 +286,7 @@ pub fn fieldInfo(comptime T: type, comptime field_name: []const u8) switch (@typ |
| 292 | 286 | TypeId.Union => TypeInfo.UnionField, |
| 293 | 287 | TypeId.ErrorSet => TypeInfo.Error, |
| 294 | 288 | TypeId.Enum => TypeInfo.EnumField, |
| 295 | else => @compileError("Expected struct, union, error set or enum type, found '" | |
| 296 | ++ @typeName(T) ++ "'"), | |
| 289 | else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"), | |
| 297 | 290 | } { |
| 298 | 291 | inline for (comptime fields(T)) |field| { |
| 299 | 292 | if (comptime mem.eql(u8, field.name, field_name)) |
| ... | ... | @@ -331,7 +324,7 @@ test "std.meta.fieldInfo" { |
| 331 | 324 | pub fn TagType(comptime T: type) type { |
| 332 | 325 | return switch (@typeInfo(T)) { |
| 333 | 326 | TypeId.Enum => |info| info.tag_type, |
| 334 | TypeId.Union => |info| if(info.tag_type) |Tag| Tag else null, | |
| 327 | TypeId.Union => |info| if (info.tag_type) |Tag| Tag else null, | |
| 335 | 328 | else => @compileError("expected enum or union type, found '" ++ @typeName(T) ++ "'"), |
| 336 | 329 | }; |
| 337 | 330 | } |
| ... | ... | @@ -350,104 +343,80 @@ test "std.meta.TagType" { |
| 350 | 343 | debug.assert(TagType(U) == E); |
| 351 | 344 | } |
| 352 | 345 | |
| 353 | ||
| 354 | ||
| 355 | 346 | ///Returns the active tag of a tagged union |
| 356 | pub fn activeTag(u: var) @TagType(@typeOf(u)) | |
| 357 | { | |
| 347 | pub fn activeTag(u: var) @TagType(@typeOf(u)) { | |
| 358 | 348 | const T = @typeOf(u); |
| 359 | 349 | return @TagType(T)(u); |
| 360 | 350 | } |
| 361 | 351 | |
| 362 | test "std.meta.activeTag" | |
| 363 | { | |
| 364 | const UE = enum. | |
| 365 | { | |
| 352 | test "std.meta.activeTag" { | |
| 353 | const UE = enum.{ | |
| 366 | 354 | Int, |
| 367 | 355 | Float, |
| 368 | 356 | }; |
| 369 | ||
| 370 | const U = union(UE). | |
| 371 | { | |
| 357 | ||
| 358 | const U = union(UE).{ | |
| 372 | 359 | Int: u32, |
| 373 | 360 | Float: f32, |
| 374 | 361 | }; |
| 375 | ||
| 376 | var u = U.{ .Int = 32, }; | |
| 362 | ||
| 363 | var u = U.{ .Int = 32 }; | |
| 377 | 364 | debug.assert(activeTag(u) == UE.Int); |
| 378 | ||
| 379 | u = U.{ .Float = 112.9876, }; | |
| 380 | debug.assert(activeTag(u) == UE.Float); | |
| 381 | 365 | |
| 366 | u = U.{ .Float = 112.9876 }; | |
| 367 | debug.assert(activeTag(u) == UE.Float); | |
| 382 | 368 | } |
| 383 | 369 | |
| 384 | 370 | ///Compares two of any type for equality. Containers are compared on a field-by-field basis, |
| 385 | 371 | /// where possible. Pointers are not followed. |
| 386 | pub fn eql(a: var, b: @typeOf(a)) bool | |
| 387 | { | |
| 372 | pub fn eql(a: var, b: @typeOf(a)) bool { | |
| 388 | 373 | const T = @typeOf(a); |
| 389 | ||
| 390 | switch(@typeId(T)) | |
| 391 | { | |
| 392 | builtin.TypeId.Struct => | |
| 393 | { | |
| 374 | ||
| 375 | switch (@typeId(T)) { | |
| 376 | builtin.TypeId.Struct => { | |
| 394 | 377 | const info = @typeInfo(T).Struct; |
| 395 | ||
| 396 | inline for(info.fields) |field_info| | |
| 397 | { | |
| 398 | if(!eql(@field(a, field_info.name), | |
| 399 | @field(b, field_info.name))) return false; | |
| 378 | ||
| 379 | inline for (info.fields) |field_info| { | |
| 380 | if (!eql(@field(a, field_info.name), @field(b, field_info.name))) return false; | |
| 400 | 381 | } |
| 401 | 382 | return true; |
| 402 | 383 | }, |
| 403 | builtin.TypeId.ErrorUnion => | |
| 404 | { | |
| 405 | if(a) |a_p| | |
| 406 | { | |
| 407 | if(b) |b_p| return eql(a_p, b_p) else |_| return false; | |
| 408 | } | |
| 409 | else |a_e| | |
| 410 | { | |
| 411 | if(b) |_| return false else |b_e| return a_e == b_e; | |
| 384 | builtin.TypeId.ErrorUnion => { | |
| 385 | if (a) |a_p| { | |
| 386 | if (b) |b_p| return eql(a_p, b_p) else |_| return false; | |
| 387 | } else |a_e| { | |
| 388 | if (b) |_| return false else |b_e| return a_e == b_e; | |
| 412 | 389 | } |
| 413 | 390 | }, |
| 414 | builtin.TypeId.Union => | |
| 415 | { | |
| 391 | builtin.TypeId.Union => { | |
| 416 | 392 | const info = @typeInfo(T).Union; |
| 417 | ||
| 418 | if(info.tag_type) |_| | |
| 419 | { | |
| 393 | ||
| 394 | if (info.tag_type) |_| { | |
| 420 | 395 | const tag_a = activeTag(a); |
| 421 | 396 | const tag_b = activeTag(b); |
| 422 | if(tag_a != tag_b) return false; | |
| 423 | ||
| 424 | inline for(info.fields) |field_info| | |
| 425 | { | |
| 397 | if (tag_a != tag_b) return false; | |
| 398 | ||
| 399 | inline for (info.fields) |field_info| { | |
| 426 | 400 | const enum_field = field_info.enum_field.?; |
| 427 | if(enum_field.value == @enumToInt(tag_a)) | |
| 428 | { | |
| 429 | return eql(@field(a, enum_field.name), | |
| 430 | @field(b, enum_field.name)); | |
| 401 | if (enum_field.value == @enumToInt(tag_a)) { | |
| 402 | return eql(@field(a, enum_field.name), @field(b, enum_field.name)); | |
| 431 | 403 | } |
| 432 | 404 | } |
| 433 | 405 | return false; |
| 434 | 406 | } |
| 435 | ||
| 407 | ||
| 436 | 408 | @compileError("cannot compare untagged union type " ++ @typeName(T)); |
| 437 | 409 | }, |
| 438 | builtin.TypeId.Array => | |
| 439 | { | |
| 440 | if(a.len != b.len) return false; | |
| 441 | for(a) |e, i| if(!eql(e, b[i])) return false; | |
| 410 | builtin.TypeId.Array => { | |
| 411 | if (a.len != b.len) return false; | |
| 412 | for (a) |e, i| | |
| 413 | if (!eql(e, b[i])) return false; | |
| 442 | 414 | return true; |
| 443 | 415 | }, |
| 444 | builtin.TypeId.Pointer => | |
| 445 | { | |
| 416 | builtin.TypeId.Pointer => { | |
| 446 | 417 | const info = @typeInfo(T).Pointer; |
| 447 | switch(info.size) | |
| 448 | { | |
| 449 | builtin.TypeInfo.Pointer.Size.One, | |
| 450 | builtin.TypeInfo.Pointer.Size.Many => return a == b, | |
| 418 | switch (info.size) { | |
| 419 | builtin.TypeInfo.Pointer.Size.One, builtin.TypeInfo.Pointer.Size.Many => return a == b, | |
| 451 | 420 | builtin.TypeInfo.Pointer.Size.Slice => return a.ptr == b.ptr and a.len == b.len, |
| 452 | 421 | } |
| 453 | 422 | }, |
| ... | ... | @@ -455,71 +424,62 @@ pub fn eql(a: var, b: @typeOf(a)) bool |
| 455 | 424 | } |
| 456 | 425 | } |
| 457 | 426 | |
| 458 | ||
| 459 | test "std.meta.eql" | |
| 460 | { | |
| 461 | const S = struct. | |
| 462 | { | |
| 427 | test "std.meta.eql" { | |
| 428 | const S = struct.{ | |
| 463 | 429 | a: u32, |
| 464 | 430 | b: f64, |
| 465 | 431 | c: [5]u8, |
| 466 | 432 | }; |
| 467 | ||
| 468 | const U = union(enum). | |
| 469 | { | |
| 433 | ||
| 434 | const U = union(enum).{ | |
| 470 | 435 | s: S, |
| 471 | 436 | f: f32, |
| 472 | 437 | }; |
| 473 | ||
| 474 | const s_1 = S. | |
| 475 | { | |
| 438 | ||
| 439 | const s_1 = S.{ | |
| 476 | 440 | .a = 134, |
| 477 | 441 | .b = 123.3, |
| 478 | 442 | .c = "12345", |
| 479 | 443 | }; |
| 480 | ||
| 481 | const s_2 = S. | |
| 482 | { | |
| 444 | ||
| 445 | const s_2 = S.{ | |
| 483 | 446 | .a = 1, |
| 484 | 447 | .b = 123.3, |
| 485 | 448 | .c = "54321", |
| 486 | 449 | }; |
| 487 | ||
| 488 | const s_3 = S. | |
| 489 | { | |
| 450 | ||
| 451 | const s_3 = S.{ | |
| 490 | 452 | .a = 134, |
| 491 | 453 | .b = 123.3, |
| 492 | 454 | .c = "12345", |
| 493 | 455 | }; |
| 494 | ||
| 495 | const u_1 = U.{ .f = 24, }; | |
| 496 | const u_2 = U.{ .s = s_1, }; | |
| 497 | const u_3 = U.{ .f = 24, }; | |
| 498 | ||
| 456 | ||
| 457 | const u_1 = U.{ .f = 24 }; | |
| 458 | const u_2 = U.{ .s = s_1 }; | |
| 459 | const u_3 = U.{ .f = 24 }; | |
| 460 | ||
| 499 | 461 | debug.assert(eql(s_1, s_3)); |
| 500 | 462 | debug.assert(eql(&s_1, &s_1)); |
| 501 | 463 | debug.assert(!eql(&s_1, &s_3)); |
| 502 | 464 | debug.assert(eql(u_1, u_3)); |
| 503 | 465 | debug.assert(!eql(u_1, u_2)); |
| 504 | ||
| 466 | ||
| 505 | 467 | var a1 = "abcdef"; |
| 506 | 468 | var a2 = "abcdef"; |
| 507 | 469 | var a3 = "ghijkl"; |
| 508 | ||
| 470 | ||
| 509 | 471 | debug.assert(eql(a1, a2)); |
| 510 | 472 | debug.assert(!eql(a1, a3)); |
| 511 | 473 | debug.assert(!eql(a1[0..], a2[0..])); |
| 512 | ||
| 513 | const EU = struct. | |
| 514 | { | |
| 515 | fn tst(err: bool) !u8 | |
| 516 | { | |
| 517 | if(err) return error.Error; | |
| 474 | ||
| 475 | const EU = struct.{ | |
| 476 | fn tst(err: bool) !u8 { | |
| 477 | if (err) return error.Error; | |
| 518 | 478 | return u8(5); |
| 519 | 479 | } |
| 520 | 480 | }; |
| 521 | ||
| 481 | ||
| 522 | 482 | debug.assert(eql(EU.tst(true), EU.tst(true))); |
| 523 | 483 | debug.assert(eql(EU.tst(false), EU.tst(false))); |
| 524 | 484 | debug.assert(!eql(EU.tst(false), EU.tst(true))); |
| 525 | } | |
| \ No newline at end of file | ||
| 485 | } |
std/meta/trait.zig+100-180| ... | ... | @@ -8,101 +8,80 @@ const meta = @import("index.zig"); |
| 8 | 8 | |
| 9 | 9 | //This is necessary if we want to return generic functions directly because of how the |
| 10 | 10 | // the type erasure works. see: #1375 |
| 11 | fn traitFnWorkaround(comptime T: type) bool | |
| 12 | { | |
| 11 | fn traitFnWorkaround(comptime T: type) bool { | |
| 13 | 12 | return false; |
| 14 | 13 | } |
| 15 | 14 | |
| 16 | 15 | pub const TraitFn = @typeOf(traitFnWorkaround); |
| 17 | ||
| 18 | 16 | /// |
| 19 | 17 | |
| 20 | 18 | //////Trait generators |
| 21 | 19 | |
| 22 | 20 | //Need TraitList because compiler can't do varargs at comptime yet |
| 23 | 21 | pub const TraitList = []const TraitFn; |
| 24 | pub fn multiTrait(comptime traits: TraitList) TraitFn | |
| 25 | { | |
| 26 | const Closure = struct. | |
| 27 | { | |
| 28 | pub fn trait(comptime T: type) bool | |
| 29 | { | |
| 30 | inline for(traits) |t| if(!t(T)) return false; | |
| 22 | pub fn multiTrait(comptime traits: TraitList) TraitFn { | |
| 23 | const Closure = struct.{ | |
| 24 | pub fn trait(comptime T: type) bool { | |
| 25 | inline for (traits) |t| | |
| 26 | if (!t(T)) return false; | |
| 31 | 27 | return true; |
| 32 | 28 | } |
| 33 | 29 | }; |
| 34 | 30 | return Closure.trait; |
| 35 | 31 | } |
| 36 | 32 | |
| 37 | test "std.meta.trait.multiTrait" | |
| 38 | { | |
| 39 | const Vector2 = struct. | |
| 40 | { | |
| 33 | test "std.meta.trait.multiTrait" { | |
| 34 | const Vector2 = struct.{ | |
| 41 | 35 | const MyType = @This(); |
| 42 | ||
| 36 | ||
| 43 | 37 | x: u8, |
| 44 | 38 | y: u8, |
| 45 | ||
| 46 | pub fn add(self: MyType, other: MyType) MyType | |
| 47 | { | |
| 48 | return MyType. | |
| 49 | { | |
| 39 | ||
| 40 | pub fn add(self: MyType, other: MyType) MyType { | |
| 41 | return MyType.{ | |
| 50 | 42 | .x = self.x + other.x, |
| 51 | 43 | .y = self.y + other.y, |
| 52 | 44 | }; |
| 53 | 45 | } |
| 54 | 46 | }; |
| 55 | ||
| 56 | const isVector = multiTrait | |
| 57 | ( | |
| 58 | TraitList. | |
| 59 | { | |
| 60 | hasFn("add"), | |
| 61 | hasField("x"), | |
| 62 | hasField("y"), | |
| 63 | } | |
| 64 | ); | |
| 47 | ||
| 48 | const isVector = multiTrait(TraitList.{ | |
| 49 | hasFn("add"), | |
| 50 | hasField("x"), | |
| 51 | hasField("y"), | |
| 52 | }); | |
| 65 | 53 | debug.assert(isVector(Vector2)); |
| 66 | 54 | debug.assert(!isVector(u8)); |
| 67 | 55 | } |
| 68 | 56 | |
| 69 | 57 | /// |
| 70 | ||
| 71 | pub fn hasDef(comptime name: []const u8) TraitFn | |
| 72 | { | |
| 73 | const Closure = struct. | |
| 74 | { | |
| 75 | pub fn trait(comptime T: type) bool | |
| 76 | { | |
| 58 | pub fn hasDef(comptime name: []const u8) TraitFn { | |
| 59 | const Closure = struct.{ | |
| 60 | pub fn trait(comptime T: type) bool { | |
| 77 | 61 | const info = @typeInfo(T); |
| 78 | const defs = switch(info) | |
| 79 | { | |
| 62 | const defs = switch (info) { | |
| 80 | 63 | builtin.TypeId.Struct => |s| s.defs, |
| 81 | 64 | builtin.TypeId.Union => |u| u.defs, |
| 82 | 65 | builtin.TypeId.Enum => |e| e.defs, |
| 83 | 66 | else => return false, |
| 84 | 67 | }; |
| 85 | 68 | |
| 86 | inline for(defs) |def| | |
| 87 | { | |
| 88 | if(mem.eql(u8, def.name, name)) return def.is_pub; | |
| 69 | inline for (defs) |def| { | |
| 70 | if (mem.eql(u8, def.name, name)) return def.is_pub; | |
| 89 | 71 | } |
| 90 | ||
| 72 | ||
| 91 | 73 | return false; |
| 92 | 74 | } |
| 93 | 75 | }; |
| 94 | 76 | return Closure.trait; |
| 95 | 77 | } |
| 96 | 78 | |
| 97 | test "std.meta.trait.hasDef" | |
| 98 | { | |
| 99 | const TestStruct = struct. | |
| 100 | { | |
| 79 | test "std.meta.trait.hasDef" { | |
| 80 | const TestStruct = struct.{ | |
| 101 | 81 | pub const value = u8(16); |
| 102 | 82 | }; |
| 103 | ||
| 104 | const TestStructFail = struct. | |
| 105 | { | |
| 83 | ||
| 84 | const TestStructFail = struct.{ | |
| 106 | 85 | const value = u8(16); |
| 107 | 86 | }; |
| 108 | 87 | |
| ... | ... | @@ -115,13 +94,10 @@ test "std.meta.trait.hasDef" |
| 115 | 94 | } |
| 116 | 95 | |
| 117 | 96 | /// |
| 118 | pub fn hasFn(comptime name: []const u8) TraitFn | |
| 119 | { | |
| 120 | const Closure = struct. | |
| 121 | { | |
| 122 | pub fn trait(comptime T: type) bool | |
| 123 | { | |
| 124 | if(!comptime hasDef(name)(T)) return false; | |
| 97 | pub fn hasFn(comptime name: []const u8) TraitFn { | |
| 98 | const Closure = struct.{ | |
| 99 | pub fn trait(comptime T: type) bool { | |
| 100 | if (!comptime hasDef(name)(T)) return false; | |
| 125 | 101 | const DefType = @typeOf(@field(T, name)); |
| 126 | 102 | const def_type_id = @typeId(DefType); |
| 127 | 103 | return def_type_id == builtin.TypeId.Fn; |
| ... | ... | @@ -130,10 +106,8 @@ pub fn hasFn(comptime name: []const u8) TraitFn |
| 130 | 106 | return Closure.trait; |
| 131 | 107 | } |
| 132 | 108 | |
| 133 | test "std.meta.trait.hasFn" | |
| 134 | { | |
| 135 | const TestStruct = struct. | |
| 136 | { | |
| 109 | test "std.meta.trait.hasFn" { | |
| 110 | const TestStruct = struct.{ | |
| 137 | 111 | pub fn useless() void {} |
| 138 | 112 | }; |
| 139 | 113 | |
| ... | ... | @@ -143,36 +117,29 @@ test "std.meta.trait.hasFn" |
| 143 | 117 | } |
| 144 | 118 | |
| 145 | 119 | /// |
| 146 | pub fn hasField(comptime name: []const u8) TraitFn | |
| 147 | { | |
| 148 | const Closure = struct. | |
| 149 | { | |
| 150 | pub fn trait(comptime T: type) bool | |
| 151 | { | |
| 120 | pub fn hasField(comptime name: []const u8) TraitFn { | |
| 121 | const Closure = struct.{ | |
| 122 | pub fn trait(comptime T: type) bool { | |
| 152 | 123 | const info = @typeInfo(T); |
| 153 | const fields = switch(info) | |
| 154 | { | |
| 124 | const fields = switch (info) { | |
| 155 | 125 | builtin.TypeId.Struct => |s| s.fields, |
| 156 | 126 | builtin.TypeId.Union => |u| u.fields, |
| 157 | 127 | builtin.TypeId.Enum => |e| e.fields, |
| 158 | 128 | else => return false, |
| 159 | 129 | }; |
| 160 | 130 | |
| 161 | inline for(fields) |field| | |
| 162 | { | |
| 163 | if(mem.eql(u8, field.name, name)) return true; | |
| 131 | inline for (fields) |field| { | |
| 132 | if (mem.eql(u8, field.name, name)) return true; | |
| 164 | 133 | } |
| 165 | ||
| 134 | ||
| 166 | 135 | return false; |
| 167 | 136 | } |
| 168 | 137 | }; |
| 169 | 138 | return Closure.trait; |
| 170 | 139 | } |
| 171 | 140 | |
| 172 | test "std.meta.trait.hasField" | |
| 173 | { | |
| 174 | const TestStruct = struct. | |
| 175 | { | |
| 141 | test "std.meta.trait.hasField" { | |
| 142 | const TestStruct = struct.{ | |
| 176 | 143 | value: u32, |
| 177 | 144 | }; |
| 178 | 145 | |
| ... | ... | @@ -184,21 +151,16 @@ test "std.meta.trait.hasField" |
| 184 | 151 | } |
| 185 | 152 | |
| 186 | 153 | /// |
| 187 | ||
| 188 | pub fn is(comptime id: builtin.TypeId) TraitFn | |
| 189 | { | |
| 190 | const Closure = struct. | |
| 191 | { | |
| 192 | pub fn trait(comptime T: type) bool | |
| 193 | { | |
| 154 | pub fn is(comptime id: builtin.TypeId) TraitFn { | |
| 155 | const Closure = struct.{ | |
| 156 | pub fn trait(comptime T: type) bool { | |
| 194 | 157 | return id == @typeId(T); |
| 195 | 158 | } |
| 196 | 159 | }; |
| 197 | 160 | return Closure.trait; |
| 198 | 161 | } |
| 199 | 162 | |
| 200 | test "std.meta.trait.is" | |
| 201 | { | |
| 163 | test "std.meta.trait.is" { | |
| 202 | 164 | debug.assert(is(builtin.TypeId.Int)(u8)); |
| 203 | 165 | debug.assert(!is(builtin.TypeId.Int)(f32)); |
| 204 | 166 | debug.assert(is(builtin.TypeId.Pointer)(*u8)); |
| ... | ... | @@ -207,39 +169,31 @@ test "std.meta.trait.is" |
| 207 | 169 | } |
| 208 | 170 | |
| 209 | 171 | /// |
| 210 | ||
| 211 | pub fn isPtrTo(comptime id: builtin.TypeId) TraitFn | |
| 212 | { | |
| 213 | const Closure = struct. | |
| 214 | { | |
| 215 | pub fn trait(comptime T: type) bool | |
| 216 | { | |
| 217 | if(!comptime isSingleItemPtr(T)) return false; | |
| 172 | pub fn isPtrTo(comptime id: builtin.TypeId) TraitFn { | |
| 173 | const Closure = struct.{ | |
| 174 | pub fn trait(comptime T: type) bool { | |
| 175 | if (!comptime isSingleItemPtr(T)) return false; | |
| 218 | 176 | return id == @typeId(meta.Child(T)); |
| 219 | 177 | } |
| 220 | 178 | }; |
| 221 | 179 | return Closure.trait; |
| 222 | 180 | } |
| 223 | 181 | |
| 224 | test "std.meta.trait.isPtrTo" | |
| 225 | { | |
| 182 | test "std.meta.trait.isPtrTo" { | |
| 226 | 183 | debug.assert(!isPtrTo(builtin.TypeId.Struct)(struct.{})); |
| 227 | 184 | debug.assert(isPtrTo(builtin.TypeId.Struct)(*struct.{})); |
| 228 | 185 | debug.assert(!isPtrTo(builtin.TypeId.Struct)(**struct.{})); |
| 229 | 186 | } |
| 230 | 187 | |
| 231 | ||
| 232 | 188 | ///////////Strait trait Fns |
| 233 | 189 | |
| 234 | 190 | //@TODO: |
| 235 | 191 | // Somewhat limited since we can't apply this logic to normal variables, fields, or |
| 236 | 192 | // Fns yet. Should be isExternType? |
| 237 | pub fn isExtern(comptime T: type) bool | |
| 238 | { | |
| 193 | pub fn isExtern(comptime T: type) bool { | |
| 239 | 194 | const Extern = builtin.TypeInfo.ContainerLayout.Extern; |
| 240 | 195 | const info = @typeInfo(T); |
| 241 | return switch(info) | |
| 242 | { | |
| 196 | return switch (info) { | |
| 243 | 197 | builtin.TypeId.Struct => |s| s.layout == Extern, |
| 244 | 198 | builtin.TypeId.Union => |u| u.layout == Extern, |
| 245 | 199 | builtin.TypeId.Enum => |e| e.layout == Extern, |
| ... | ... | @@ -247,8 +201,7 @@ pub fn isExtern(comptime T: type) bool |
| 247 | 201 | }; |
| 248 | 202 | } |
| 249 | 203 | |
| 250 | test "std.meta.trait.isExtern" | |
| 251 | { | |
| 204 | test "std.meta.trait.isExtern" { | |
| 252 | 205 | const TestExStruct = extern struct.{}; |
| 253 | 206 | const TestStruct = struct.{}; |
| 254 | 207 | |
| ... | ... | @@ -258,13 +211,10 @@ test "std.meta.trait.isExtern" |
| 258 | 211 | } |
| 259 | 212 | |
| 260 | 213 | /// |
| 261 | ||
| 262 | pub fn isPacked(comptime T: type) bool | |
| 263 | { | |
| 214 | pub fn isPacked(comptime T: type) bool { | |
| 264 | 215 | const Packed = builtin.TypeInfo.ContainerLayout.Packed; |
| 265 | 216 | const info = @typeInfo(T); |
| 266 | return switch(info) | |
| 267 | { | |
| 217 | return switch (info) { | |
| 268 | 218 | builtin.TypeId.Struct => |s| s.layout == Packed, |
| 269 | 219 | builtin.TypeId.Union => |u| u.layout == Packed, |
| 270 | 220 | builtin.TypeId.Enum => |e| e.layout == Packed, |
| ... | ... | @@ -272,8 +222,7 @@ pub fn isPacked(comptime T: type) bool |
| 272 | 222 | }; |
| 273 | 223 | } |
| 274 | 224 | |
| 275 | test "std.meta.trait.isPacked" | |
| 276 | { | |
| 225 | test "std.meta.trait.isPacked" { | |
| 277 | 226 | const TestPStruct = packed struct.{}; |
| 278 | 227 | const TestStruct = struct.{}; |
| 279 | 228 | |
| ... | ... | @@ -283,19 +232,15 @@ test "std.meta.trait.isPacked" |
| 283 | 232 | } |
| 284 | 233 | |
| 285 | 234 | /// |
| 286 | ||
| 287 | pub fn isSingleItemPtr(comptime T: type) bool | |
| 288 | { | |
| 289 | if(comptime is(builtin.TypeId.Pointer)(T)) | |
| 290 | { | |
| 235 | pub fn isSingleItemPtr(comptime T: type) bool { | |
| 236 | if (comptime is(builtin.TypeId.Pointer)(T)) { | |
| 291 | 237 | const info = @typeInfo(T); |
| 292 | 238 | return info.Pointer.size == builtin.TypeInfo.Pointer.Size.One; |
| 293 | 239 | } |
| 294 | 240 | return false; |
| 295 | 241 | } |
| 296 | 242 | |
| 297 | test "std.meta.trait.isSingleItemPtr" | |
| 298 | { | |
| 243 | test "std.meta.trait.isSingleItemPtr" { | |
| 299 | 244 | const array = []u8.{0} ** 10; |
| 300 | 245 | debug.assert(isSingleItemPtr(@typeOf(&array[0]))); |
| 301 | 246 | debug.assert(!isSingleItemPtr(@typeOf(array))); |
| ... | ... | @@ -303,19 +248,15 @@ test "std.meta.trait.isSingleItemPtr" |
| 303 | 248 | } |
| 304 | 249 | |
| 305 | 250 | /// |
| 306 | ||
| 307 | pub fn isManyItemPtr(comptime T: type) bool | |
| 308 | { | |
| 309 | if(comptime is(builtin.TypeId.Pointer)(T)) | |
| 310 | { | |
| 251 | pub fn isManyItemPtr(comptime T: type) bool { | |
| 252 | if (comptime is(builtin.TypeId.Pointer)(T)) { | |
| 311 | 253 | const info = @typeInfo(T); |
| 312 | 254 | return info.Pointer.size == builtin.TypeInfo.Pointer.Size.Many; |
| 313 | 255 | } |
| 314 | 256 | return false; |
| 315 | 257 | } |
| 316 | 258 | |
| 317 | test "std.meta.trait.isManyItemPtr" | |
| 318 | { | |
| 259 | test "std.meta.trait.isManyItemPtr" { | |
| 319 | 260 | const array = []u8.{0} ** 10; |
| 320 | 261 | const mip = @ptrCast([*]const u8, &array[0]); |
| 321 | 262 | debug.assert(isManyItemPtr(@typeOf(mip))); |
| ... | ... | @@ -324,19 +265,15 @@ test "std.meta.trait.isManyItemPtr" |
| 324 | 265 | } |
| 325 | 266 | |
| 326 | 267 | /// |
| 327 | ||
| 328 | pub fn isSlice(comptime T: type) bool | |
| 329 | { | |
| 330 | if(comptime is(builtin.TypeId.Pointer)(T)) | |
| 331 | { | |
| 268 | pub fn isSlice(comptime T: type) bool { | |
| 269 | if (comptime is(builtin.TypeId.Pointer)(T)) { | |
| 332 | 270 | const info = @typeInfo(T); |
| 333 | 271 | return info.Pointer.size == builtin.TypeInfo.Pointer.Size.Slice; |
| 334 | 272 | } |
| 335 | 273 | return false; |
| 336 | 274 | } |
| 337 | 275 | |
| 338 | test "std.meta.trait.isSlice" | |
| 339 | { | |
| 276 | test "std.meta.trait.isSlice" { | |
| 340 | 277 | const array = []u8.{0} ** 10; |
| 341 | 278 | debug.assert(isSlice(@typeOf(array[0..]))); |
| 342 | 279 | debug.assert(!isSlice(@typeOf(array))); |
| ... | ... | @@ -344,27 +281,22 @@ test "std.meta.trait.isSlice" |
| 344 | 281 | } |
| 345 | 282 | |
| 346 | 283 | /// |
| 347 | ||
| 348 | pub fn isIndexable(comptime T: type) bool | |
| 349 | { | |
| 350 | if(comptime is(builtin.TypeId.Pointer)(T)) | |
| 351 | { | |
| 352 | const info = @typeInfo(T); | |
| 353 | if(info.Pointer.size == builtin.TypeInfo.Pointer.Size.One) | |
| 354 | { | |
| 355 | if(comptime is(builtin.TypeId.Array)(meta.Child(T))) return true; | |
| 356 | return false; | |
| 357 | } | |
| 358 | return true; | |
| 359 | } | |
| 360 | return comptime is(builtin.TypeId.Array)(T); | |
| 284 | pub fn isIndexable(comptime T: type) bool { | |
| 285 | if (comptime is(builtin.TypeId.Pointer)(T)) { | |
| 286 | const info = @typeInfo(T); | |
| 287 | if (info.Pointer.size == builtin.TypeInfo.Pointer.Size.One) { | |
| 288 | if (comptime is(builtin.TypeId.Array)(meta.Child(T))) return true; | |
| 289 | return false; | |
| 290 | } | |
| 291 | return true; | |
| 292 | } | |
| 293 | return comptime is(builtin.TypeId.Array)(T); | |
| 361 | 294 | } |
| 362 | 295 | |
| 363 | test "std.meta.trait.isIndexable" | |
| 364 | { | |
| 296 | test "std.meta.trait.isIndexable" { | |
| 365 | 297 | const array = []u8.{0} ** 10; |
| 366 | 298 | const slice = array[0..]; |
| 367 | ||
| 299 | ||
| 368 | 300 | debug.assert(isIndexable(@typeOf(array))); |
| 369 | 301 | debug.assert(isIndexable(@typeOf(&array))); |
| 370 | 302 | debug.assert(isIndexable(@typeOf(slice))); |
| ... | ... | @@ -372,26 +304,18 @@ test "std.meta.trait.isIndexable" |
| 372 | 304 | } |
| 373 | 305 | |
| 374 | 306 | /// |
| 375 | ||
| 376 | pub fn isNumber(comptime T: type) bool | |
| 377 | { | |
| 378 | return switch(@typeId(T)) | |
| 379 | { | |
| 380 | builtin.TypeId.Int, | |
| 381 | builtin.TypeId.Float, | |
| 382 | builtin.TypeId.ComptimeInt, | |
| 383 | builtin.TypeId.ComptimeFloat => true, | |
| 307 | pub fn isNumber(comptime T: type) bool { | |
| 308 | return switch (@typeId(T)) { | |
| 309 | builtin.TypeId.Int, builtin.TypeId.Float, builtin.TypeId.ComptimeInt, builtin.TypeId.ComptimeFloat => true, | |
| 384 | 310 | else => false, |
| 385 | 311 | }; |
| 386 | 312 | } |
| 387 | 313 | |
| 388 | test "std.meta.trait.isNumber" | |
| 389 | { | |
| 390 | const NotANumber = struct. | |
| 391 | { | |
| 314 | test "std.meta.trait.isNumber" { | |
| 315 | const NotANumber = struct.{ | |
| 392 | 316 | number: u8, |
| 393 | 317 | }; |
| 394 | ||
| 318 | ||
| 395 | 319 | debug.assert(isNumber(u32)); |
| 396 | 320 | debug.assert(isNumber(f32)); |
| 397 | 321 | debug.assert(isNumber(u64)); |
| ... | ... | @@ -402,16 +326,13 @@ test "std.meta.trait.isNumber" |
| 402 | 326 | } |
| 403 | 327 | |
| 404 | 328 | /// |
| 405 | ||
| 406 | pub fn isConstPtr(comptime T: type) bool | |
| 407 | { | |
| 408 | if(!comptime is(builtin.TypeId.Pointer)(T)) return false; | |
| 329 | pub fn isConstPtr(comptime T: type) bool { | |
| 330 | if (!comptime is(builtin.TypeId.Pointer)(T)) return false; | |
| 409 | 331 | const info = @typeInfo(T); |
| 410 | 332 | return info.Pointer.is_const; |
| 411 | 333 | } |
| 412 | 334 | |
| 413 | test "std.meta.trait.isConstPtr" | |
| 414 | { | |
| 335 | test "std.meta.trait.isConstPtr" { | |
| 415 | 336 | var t = u8(0); |
| 416 | 337 | const c = u8(0); |
| 417 | 338 | debug.assert(isConstPtr(*const @typeOf(t))); |
| ... | ... | @@ -421,12 +342,9 @@ test "std.meta.trait.isConstPtr" |
| 421 | 342 | } |
| 422 | 343 | |
| 423 | 344 | /// |
| 424 | ||
| 425 | pub fn isContainer(comptime T: type) bool | |
| 426 | { | |
| 345 | pub fn isContainer(comptime T: type) bool { | |
| 427 | 346 | const info = @typeInfo(T); |
| 428 | return switch(info) | |
| 429 | { | |
| 347 | return switch (info) { | |
| 430 | 348 | builtin.TypeId.Struct => true, |
| 431 | 349 | builtin.TypeId.Union => true, |
| 432 | 350 | builtin.TypeId.Enum => true, |
| ... | ... | @@ -434,16 +352,18 @@ pub fn isContainer(comptime T: type) bool |
| 434 | 352 | }; |
| 435 | 353 | } |
| 436 | 354 | |
| 437 | test "std.meta.trait.isContainer" | |
| 438 | { | |
| 355 | test "std.meta.trait.isContainer" { | |
| 439 | 356 | const TestStruct = struct.{}; |
| 440 | const TestUnion = union.{ a: void, }; | |
| 441 | const TestEnum = enum.{ A, B, }; | |
| 442 | ||
| 357 | const TestUnion = union.{ | |
| 358 | a: void, | |
| 359 | }; | |
| 360 | const TestEnum = enum.{ | |
| 361 | A, | |
| 362 | B, | |
| 363 | }; | |
| 364 | ||
| 443 | 365 | debug.assert(isContainer(TestStruct)); |
| 444 | 366 | debug.assert(isContainer(TestUnion)); |
| 445 | 367 | debug.assert(isContainer(TestEnum)); |
| 446 | 368 | debug.assert(!isContainer(u8)); |
| 447 | 369 | } |
| 448 | ||
| 449 | /// | |
| \ No newline at end of file |