authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-04-15 03:14:15 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-04-15 03:14:15 -07:00
log9abeb2ac150728409877a3d0003e9a009acfc176
treed0446c170171f5cc00901c21af81012745175cb8
parent3b9df8487b38849f5b36fc8b81fbaf332680464b

add StructOfSlices


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

src/StructOfSlices.zig created+39
...@@ -0,0 +1,39 @@
1const std = @import("std");
2const string = []const u8;
3const extras = @import("./lib.zig");
4const expectSimilarType = extras.expectSimilarType;
5
6pub fn StructOfSlices(T: type) type {
7 const fields = std.meta.fields(T);
8 var new_fields: [fields.len]std.builtin.Type.StructField = undefined;
9 for (fields, 0..) |item, i| {
10 new_fields[i] = .{
11 .name = item.name,
12 .type = []const item.type,
13 .default_value = null,
14 .is_comptime = false,
15 .alignment = @alignOf([]const item.type),
16 };
17 }
18 const result = new_fields[0..fields.len];
19 return @Type(@unionInit(std.builtin.Type, "Struct", .{
20 .layout = .auto,
21 .backing_integer = null,
22 .fields = result,
23 .decls = &.{},
24 .is_tuple = false,
25 }));
26}
27
28test {
29 try expectSimilarType(
30 StructOfSlices(struct {
31 x: u8,
32 y: u16,
33 }),
34 struct {
35 x: []const u8,
36 y: []const u16,
37 },
38 );
39}
src/lib.zig+1
...@@ -105,3 +105,4 @@ pub usingnamespace @import("./lessThanBy.zig");...@@ -105,3 +105,4 @@ pub usingnamespace @import("./lessThanBy.zig");
105pub usingnamespace @import("./isContainer.zig");105pub usingnamespace @import("./isContainer.zig");
106pub usingnamespace @import("./hasFn.zig");106pub usingnamespace @import("./hasFn.zig");
107pub usingnamespace @import("./hasFields.zig");107pub usingnamespace @import("./hasFields.zig");
108pub usingnamespace @import("./StructOfSlices.zig");