authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-09-21 17:24:02 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-09-21 17:24:02 -07:00
logf8fbafb2ada9092bc426e3e62c4cfce8f30a5a47
tree40b9e20e6a9141c767ed7565e1e377d2c06b64d9
parent9f2ec2b07f59d5ed208f66c880bc8b3909bf1049

add isIndexable


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

src/isIndexable.zig created+38
...@@ -0,0 +1,38 @@
1const std = @import("std");
2const string = []const u8;
3const extras = @import("./lib.zig");
4
5pub 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}
14fn 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}
22fn isTuple(comptime T: type) bool {
23 return is(.Struct)(T) and @typeInfo(T).Struct.is_tuple;
24}
25
26test {
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}
src/lib.zig+1
...@@ -96,3 +96,4 @@ pub usingnamespace @import("./globalOption.zig");...@@ -96,3 +96,4 @@ pub usingnamespace @import("./globalOption.zig");
96pub usingnamespace @import("./OneSmallerInt.zig");96pub 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");