summaryrefslogtreecommitdiff
path: root/src/List.zig
blob: 6c9f65c8f2a1857f67d7f04e636388dd84e34a42 (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
38
39
40
41
42
43
44
45
46
47
48
49
const c = @import("c");

const zpy = @import("root.zig");
const conversion = zpy.conversion;

const Self = @This();

object: zpy.Object,

pub fn new(items: []const zpy.Object) !Self {
    var list: Self = .{
        .object = .fromPtr(try conversion.ptr(c.PyList_New(items.len))),
    };
    errdefer list.deinit();

    for (items, 0..) |item, index| {
        try list.insert(index, item);
    }
}

pub fn size(self: *const Self) usize {
    return @as(usize, c.PyList_Size(self.object.ptr));
}

pub fn get(self: *const Self, index: usize) !zpy.Object {
    return .fromC(try conversion.ptr(c.PyList_GetItem(self.object.ptr, index)));
}

pub fn set(self: *Self, index: usize, value: zpy.Object) !void {
    return conversion.success(
        c.PyList_SetItem(self.object.ptr, index, value.ptr),
    );
}

pub fn insert(self: *Self, index: usize, value: zpy.Object) !void {
    return .conversion.success(
        c.PyList_Insert(self.object.ptr, index, value.ptr)
    );
}

pub fn append(self: *Self, value: zpy.Object) !void {
    return .conversion.success(
        c.PyList_Append(self.object.ptr, value.ptr),
    );
}

pub fn deinit(self: *Self) void {
    self.object.decref();
}