diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-08-31 23:05:16 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-08-31 23:05:16 +0200 |
| commit | 804204db7cf1e8a28defd359d17fa72949e13eff (patch) | |
| tree | 3536d961838d1a6640d1295d808768736bf2317b /src/tuple.zig | |
| parent | 956cd84ca545f1d93e2218cbbdbb39c81a46bec8 (diff) | |
Implement with ptrCast to make it more transparent.
Diffstat (limited to 'src/tuple.zig')
| -rw-r--r-- | src/tuple.zig | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/tuple.zig b/src/tuple.zig new file mode 100644 index 0000000..ba82003 --- /dev/null +++ b/src/tuple.zig @@ -0,0 +1,46 @@ +const c = @import("c"); +const zpy = @import("root.zig"); + +pub const Tuple = extern struct { + const Self = @This(); + + data: c.PyObject, + + pub fn new(length: usize) !*Self { + return @ptrCast(c.PyTuple_New(@intCast(length)) orelse return error.Exception); + } + + pub fn pack(values: []const *zpy.Object) !*Self { + var tuple = try new(values.len); + errdefer tuple.asObject().decref(); + + for (values, 0..) |value, index| { + try tuple.set(index, value); + } + + return tuple; + } + + pub fn size(self: *const Self) usize { + return c.PyTuple_GET_SIZE(self.object.ptr); + } + + pub fn get(self: *Self, index: usize) !*zpy.Object { + return @ptrCast( + c.PyTuple_GetItem( + self.object.ptr, + @intCast(index), + ) orelse return error.Exception, + ); + } + + pub fn set(self: *Self, index: usize, value: *zpy.Object) !void { + if (c.PyTuple_SetItem(@ptrCast(self), @intCast(index), @ptrCast(value)) == -1) { + return error.Exception; + } + } + + pub fn asObject(self: *Self) *zpy.Object { + return @ptrCast(self); + } +}; |