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