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