-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzig.zig
38 lines (36 loc) · 1.21 KB
/
zig.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const std = @import("std");
const FnParam = std.builtin.Type.Fn.Param;
/// generate a fuction's param tuple
pub fn FnParamsToTuple(comptime params: []const FnParam) type {
const Type = std.builtin.Type;
const fields: [params.len]Type.StructField = blk: {
var res: [params.len]Type.StructField = undefined;
for (params, 0..params.len) |param, i| {
if (param.type) |t| {
res[i] = Type.StructField{
.type = t,
.alignment = @alignOf(t),
.default_value = null,
.is_comptime = false,
.name = std.fmt.comptimePrint("{}", .{i}),
};
} else {
// when param type is anytype, the type is null
const error_message = std.fmt.comptimePrint(
"sorry the param is anytype!",
.{param},
);
@compileError(error_message);
}
}
break :blk res;
};
return @Type(.{
.Struct = std.builtin.Type.Struct{
.layout = .Auto,
.is_tuple = true,
.decls = &.{},
.fields = &fields,
},
});
}