diff --git a/src/StructOfSlices.zig b/src/StructOfSlices.zig new file mode 100644 index 0000000000000000000000000000000000000000..5f9be7e6a3a7cc01980f63b5f0876017fa636118 --- /dev/null +++ b/src/StructOfSlices.zig @@ -0,0 +1,39 @@ +const std = @import("std"); +const string = []const u8; +const extras = @import("./lib.zig"); +const expectSimilarType = extras.expectSimilarType; + +pub fn StructOfSlices(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 = []const item.type, + .default_value = null, + .is_comptime = false, + .alignment = @alignOf([]const 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( + StructOfSlices(struct { + x: u8, + y: u16, + }), + struct { + x: []const u8, + y: []const u16, + }, + ); +} diff --git a/src/lib.zig b/src/lib.zig index 68dfceb2f5c6806e8941067a8981283b5a7735cb..b38046819fa2e0838eacf6838c52e79fa37a720f 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -105,3 +105,4 @@ pub usingnamespace @import("./lessThanBy.zig"); pub usingnamespace @import("./isContainer.zig"); pub usingnamespace @import("./hasFn.zig"); pub usingnamespace @import("./hasFields.zig"); +pub usingnamespace @import("./StructOfSlices.zig");