mirror of
https://github.com/ghostty-org/ghostty.git
synced 2026-05-28 15:55:20 +00:00
lib: add a TaggedUnion helper to create C ABI compatible tagged unions
This commit is contained in:
31
src/lib/struct.zig
Normal file
31
src/lib/struct.zig
Normal file
@@ -0,0 +1,31 @@
|
||||
const std = @import("std");
|
||||
const Target = @import("target.zig").Target;
|
||||
|
||||
pub fn Struct(
|
||||
comptime target: Target,
|
||||
comptime Zig: type,
|
||||
) type {
|
||||
return switch (target) {
|
||||
.zig => Zig,
|
||||
.c => c: {
|
||||
const info = @typeInfo(Zig).@"struct";
|
||||
var fields: [info.fields.len]std.builtin.Type.StructField = undefined;
|
||||
for (info.fields, 0..) |field, i| {
|
||||
fields[i] = .{
|
||||
.name = field.name,
|
||||
.type = field.type,
|
||||
.default_value_ptr = field.default_value_ptr,
|
||||
.is_comptime = field.is_comptime,
|
||||
.alignment = field.alignment,
|
||||
};
|
||||
}
|
||||
|
||||
break :c @Type(.{ .@"struct" = .{
|
||||
.layout = .@"extern",
|
||||
.fields = &fields,
|
||||
.decls = &.{},
|
||||
.is_tuple = info.is_tuple,
|
||||
} });
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user