summaryrefslogtreecommitdiff
path: root/src/Tuple.zig
blob: af1ec805dfb70ef9355ab2af36b320c644ef64e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const c = @import("c");
const zpy = @import("root.zig");
const conversion = zpy.conversion;

const Self = @This();

object: zpy.Object,

pub fn new(length: usize) !Self {
    return .{
        .object = .fromPtr(try conversion.ptr(c.PyTuple_New(@intCast(length)))),
    };
}

pub fn pack(values: []const zpy.Object) !Self {
    var tuple = try new(values.len);
    errdefer tuple.object.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.Error!zpy.Object {
    return .fromPtr(conversion.ptr(c.PyTuple_GetItem(self.object.ptr, @intCast(index))) catch return error.IndexError);
}

pub fn set(self: *Self, index: usize, value: zpy.Object) zpy.Error!void {
    conversion.success(c.PyTuple_SetItem(self.object.ptr, @intCast(index), value.ptr)) catch return error.IndexError;
}