| ... | @@ -0,0 +1,38 @@ |
| 1 | const std = @import("std"); |
| 2 | const string = []const u8; |
| 3 | const extras = @import("./lib.zig"); |
| 4 | |
| 5 | pub fn isIndexable(comptime T: type) bool { |
| 6 | if (comptime is(.Pointer)(T)) { |
| 7 | if (@typeInfo(T).Pointer.size == .One) { |
| 8 | return (comptime is(.Array)(std.meta.Child(T))); |
| 9 | } |
| 10 | return true; |
| 11 | } |
| 12 | return comptime is(.Array)(T) or is(.Vector)(T) or isTuple(T); |
| 13 | } |
| 14 | fn is(comptime id: std.builtin.TypeId) fn (type) bool { |
| 15 | const Closure = struct { |
| 16 | pub fn trait(comptime T: type) bool { |
| 17 | return id == @typeInfo(T); |
| 18 | } |
| 19 | }; |
| 20 | return Closure.trait; |
| 21 | } |
| 22 | fn isTuple(comptime T: type) bool { |
| 23 | return is(.Struct)(T) and @typeInfo(T).Struct.is_tuple; |
| 24 | } |
| 25 | |
| 26 | test { |
| 27 | const array = [_]u8{0} ** 10; |
| 28 | const slice = @as([]const u8, &array); |
| 29 | const vector: @Vector(2, u32) = [_]u32{0} ** 2; |
| 30 | const tuple = .{ 1, 2, 3 }; |
| 31 | |
| 32 | try std.testing.expect(isIndexable(@TypeOf(array))); |
| 33 | try std.testing.expect(isIndexable(@TypeOf(&array))); |
| 34 | try std.testing.expect(isIndexable(@TypeOf(slice))); |
| 35 | try std.testing.expect(!isIndexable(std.meta.Child(@TypeOf(slice)))); |
| 36 | try std.testing.expect(isIndexable(@TypeOf(vector))); |
| 37 | try std.testing.expect(isIndexable(@TypeOf(tuple))); |
| 38 | } |