From 787a447079555cc8170a1845f80f4bd9f9047aa2 Mon Sep 17 00:00:00 2001 From: Ian Johnson Date: Sat, 13 Jul 2024 16:15:27 -0400 Subject: [PATCH] feat: add `glib.ext.new` --- extensions/glib2.zig | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/extensions/glib2.zig b/extensions/glib2.zig index b684820..fb2ee1b 100644 --- a/extensions/glib2.zig +++ b/extensions/glib2.zig @@ -9,6 +9,14 @@ pub fn create(comptime T: type) *T { return @ptrCast(@alignCast(glib.malloc(@sizeOf(T)))); } +/// Creates a heap-allocated copy of `value` using `glib.malloc`. `T` must not +/// be zero-sized or aligned more than `std.c.max_align_t`. +pub inline fn new(comptime T: type, value: T) *T { + const new_value = create(T); + new_value.* = value; + return new_value; +} + /// Destroys a value created using `create`. pub fn destroy(ptr: anytype) void { const type_info = @typeInfo(@TypeOf(ptr));