1const std = @import("std");
2const string = []const u8;
3const extras = @import("./lib.zig");
4const expectSimilarType = extras.expectSimilarType;
5
6pub fn StructOfArrays(len: usize, T: type) type {
7 const info = @typeInfo(T).@"struct";
8 const fields = info.fields;
9 var names: [fields.len][]const u8 = undefined;
10 var types: [fields.len]type = undefined;
11 var attrs: [fields.len]std.builtin.Type.StructField.Attributes = undefined;
12 for (fields, 0..) |item, i| {
13 names[i] = item.name;
14 types[i] = [len]item.type;
15 attrs[i] = .{
16 .@"comptime" = item.is_comptime,
17 .@"align" = item.alignment,
18 .default_value_ptr = item.default_value_ptr,
19 };
20 }
21 if (info.is_tuple) return @Tuple(&types);
22 return @Struct(.auto, null, &names, &types, &attrs);
23}
24
25test {
26 try expectSimilarType(
27 StructOfArrays(10, struct {
28 x: u8,
29 y: u16,
30 }),
31 struct {
32 x: [10]u8,
33 y: [10]u16,
34 },
35 );
36}