diff --git a/src/StructOfArrays.zig b/src/StructOfArrays.zig new file mode 100644 index 0000000000000000000000000000000000000000..a7720c80702cf98910ea8b31fc0715e2ae8795c5 --- /dev/null +++ b/src/StructOfArrays.zig @@ -0,0 +1,39 @@ +const std = @import("std"); +const string = []const u8; +const extras = @import("./lib.zig"); +const expectSimilarType = extras.expectSimilarType; + +pub fn StructOfArrays(len: usize, T: type) type { + const fields = std.meta.fields(T); + var new_fields: [fields.len]std.builtin.Type.StructField = undefined; + for (fields, 0..) |item, i| { + new_fields[i] = .{ + .name = item.name, + .type = [len]item.type, + .default_value = null, + .is_comptime = false, + .alignment = @alignOf([len]item.type), + }; + } + const result = new_fields[0..fields.len]; + return @Type(@unionInit(std.builtin.Type, "Struct", .{ + .layout = .auto, + .backing_integer = null, + .fields = result, + .decls = &.{}, + .is_tuple = false, + })); +} + +test { + try expectSimilarType( + StructOfArrays(10, struct { + x: u8, + y: u16, + }), + struct { + x: [10]u8, + y: [10]u16, + }, + ); +} diff --git a/src/lib.zig b/src/lib.zig index b38046819fa2e0838eacf6838c52e79fa37a720f..274c340ca504191044ad7fff70c9fb548d8d843f 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -106,3 +106,4 @@ pub usingnamespace @import("./isContainer.zig"); pub usingnamespace @import("./hasFn.zig"); pub usingnamespace @import("./hasFields.zig"); pub usingnamespace @import("./StructOfSlices.zig"); +pub usingnamespace @import("./StructOfArrays.zig");