| author | |
| committer | |
| log | 4f8933255778838a5f9a34cade2942e9aeae8415 |
| tree | 2cdee055f782eef9c2919e054e3cc964f8eb358d |
| parent | f8fbafb2ada9092bc426e3e62c4cfce8f30a5a47 |
2 files changed, 28 insertions(+), 0 deletions(-)
src/isSlice.zig created+27| ... | @@ -0,0 +1,27 @@ | ||
| 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 | return @typeInfo(T).Pointer.size == .Slice; | ||
| 8 | } | ||
| 9 | return false; | ||
| 10 | } | ||
| 11 | fn is(comptime id: std.builtin.TypeId) fn (type) bool { | ||
| 12 | const Closure = struct { | ||
| 13 | pub fn trait(comptime T: type) bool { | ||
| 14 | return id == @typeInfo(T); | ||
| 15 | } | ||
| 16 | }; | ||
| 17 | return Closure.trait; | ||
| 18 | } | ||
| 19 | |||
| 20 | test { | ||
| 21 | const array = [_]u8{0} ** 10; | ||
| 22 | var runtime_zero: usize = 0; | ||
| 23 | _ = &runtime_zero; | ||
| 24 | try std.testing.expect(isSlice(@TypeOf(array[runtime_zero..]))); | ||
| 25 | try std.testing.expect(!isSlice(@TypeOf(array))); | ||
| 26 | try std.testing.expect(!isSlice(@TypeOf(&array[0]))); | ||
| 27 | } | ||
src/lib.zig+1| ... | @@ -97,3 +97,4 @@ pub usingnamespace @import("./OneSmallerInt.zig"); | ... | @@ -97,3 +97,4 @@ pub usingnamespace @import("./OneSmallerInt.zig"); |
| 97 | pub usingnamespace @import("./FlippedInt.zig"); | 97 | pub usingnamespace @import("./FlippedInt.zig"); |
| 98 | pub usingnamespace @import("./isZigString.zig"); | 98 | pub usingnamespace @import("./isZigString.zig"); |
| 99 | pub usingnamespace @import("./isIndexable.zig"); | 99 | pub usingnamespace @import("./isIndexable.zig"); |
| 100 | pub usingnamespace @import("./isSlice.zig"); |