| ... | @@ -0,0 +1,44 @@ |
| 1 | const std = @import("std"); |
| 2 | const string = []const u8; |
| 3 | const extras = @import("./lib.zig"); |
| 4 | const StructOfSlices = extras.StructOfSlices; |
| 5 | const StructOfArrays = extras.StructOfArrays; |
| 6 | const expectSimilarType = extras.expectSimilarType; |
| 7 | |
| 8 | pub fn MultiArray(T: type) type { |
| 9 | return struct { |
| 10 | items: StructOfSlices(T), |
| 11 | |
| 12 | pub fn initComptime(comptime data: []const T) @This() { |
| 13 | return comptime blk: { |
| 14 | var temp: StructOfArrays(data.len, T) = undefined; |
| 15 | for (data, 0..) |item, i| { |
| 16 | for (std.meta.fieldNames(T)) |name| { |
| 17 | @field(temp, name)[i] = @field(item, name); |
| 18 | } |
| 19 | } |
| 20 | var result: @This() = undefined; |
| 21 | for (std.meta.fields(T)) |field| { |
| 22 | const constant = @field(temp, field.name)[0..data.len].*; |
| 23 | @field(result.items, field.name) = &constant; |
| 24 | } |
| 25 | break :blk result; |
| 26 | }; |
| 27 | } |
| 28 | }; |
| 29 | } |
| 30 | |
| 31 | test { |
| 32 | try expectSimilarType( |
| 33 | MultiArray(struct { |
| 34 | x: u8, |
| 35 | y: u16, |
| 36 | }), |
| 37 | struct { |
| 38 | items: struct { |
| 39 | x: []const u8, |
| 40 | y: []const u16, |
| 41 | }, |
| 42 | }, |
| 43 | ); |
| 44 | } |