summaryrefslogtreecommitdiff
path: root/src/List.zig
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2026-08-28 17:44:38 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2026-08-28 17:44:38 +0200
commit956cd84ca545f1d93e2218cbbdbb39c81a46bec8 (patch)
tree21e865b4af06acce77a0623ea5dd3a661306f21e /src/List.zig
parentdf52109ff18ff354e4c0d1b003bc1a0a42c42b54 (diff)
implement other builtins
Diffstat (limited to 'src/List.zig')
-rw-r--r--src/List.zig49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/List.zig b/src/List.zig
new file mode 100644
index 0000000..6c9f65c
--- /dev/null
+++ b/src/List.zig
@@ -0,0 +1,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();
+}