authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-09-21 17:24:13 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-09-21 17:24:13 -07:00
log4f8933255778838a5f9a34cade2942e9aeae8415
tree2cdee055f782eef9c2919e054e3cc964f8eb358d
parentf8fbafb2ada9092bc426e3e62c4cfce8f30a5a47

add isSlice


2 files changed, 28 insertions(+), 0 deletions(-)

src/isSlice.zig created+27
...@@ -0,0 +1,27 @@
1const std = @import("std");
2const string = []const u8;
3const extras = @import("./lib.zig");
4
5pub fn isSlice(comptime T: type) bool {
6 if (comptime is(.Pointer)(T)) {
7 return @typeInfo(T).Pointer.size == .Slice;
8 }
9 return false;
10}
11fn 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
20test {
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");
97pub usingnamespace @import("./FlippedInt.zig");97pub usingnamespace @import("./FlippedInt.zig");
98pub usingnamespace @import("./isZigString.zig");98pub usingnamespace @import("./isZigString.zig");
99pub usingnamespace @import("./isIndexable.zig");99pub usingnamespace @import("./isIndexable.zig");
100pub usingnamespace @import("./isSlice.zig");