summaryrefslogtreecommitdiff
path: root/src/int.zig
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2026-08-31 23:05:16 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2026-08-31 23:05:16 +0200
commit804204db7cf1e8a28defd359d17fa72949e13eff (patch)
tree3536d961838d1a6640d1295d808768736bf2317b /src/int.zig
parent956cd84ca545f1d93e2218cbbdbb39c81a46bec8 (diff)
Implement with ptrCast to make it more transparent.
Diffstat (limited to 'src/int.zig')
-rw-r--r--src/int.zig40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/int.zig b/src/int.zig
new file mode 100644
index 0000000..d5e7589
--- /dev/null
+++ b/src/int.zig
@@ -0,0 +1,40 @@
+const std = @import("std");
+const c = @import("c");
+
+const zpy = @import("root.zig");
+
+pub const Int = extern struct {
+ const Self = @This();
+
+ data: c.PyObject,
+
+ pub fn from(value: anytype) !*Self {
+ const T = @TypeOf(value);
+ const result = switch (@typeInfo(T)) {
+ .int => |i| switch (i.signedness) {
+ .signed => c.PyLong_FromLong(value),
+ .unsigned => c.PyLong_FromUnsignedLong(value),
+ },
+ .comptime_int => c.PyLong_FromLong(value),
+ .float => c.PyLong_FromDouble(value),
+ else => @compileError("value must be numberic."),
+ };
+
+ return @ptrCast(result orelse return error.Exception);
+ }
+
+ pub fn as(self: *const Self, T: type) T {
+ return switch(@typeInfo(T)) {
+ .int => |i| switch (i) {
+ .signed => c.PyLong_AsLong(self.object.ptr),
+ .unsigned => c.PyLong_AsUnsignedLong(self.object.ptr),
+ },
+ .float => c.PyLong_AsDouble(self.object.ptr),
+ else => @compileError("value must be numberic."),
+ };
+ }
+
+ pub fn asObject(self: *Self) *zpy.Object {
+ return @ptrCast(self);
+ }
+};