aboutsummaryrefslogtreecommitdiff
path: root/src/zilch/Env/future.zig
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2026-10-12 09:43:19 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2026-10-12 09:43:19 +0200
commit0b2af471b6d5dfc4eaf53639102c1b854bbb324b (patch)
treeaba3943b8c54d26d8552d0685d4178bc995d91c3 /src/zilch/Env/future.zig
parent812f547d84b5ce1a2924ae16f6257e4fd19efcc0 (diff)
first version of zilch
Diffstat (limited to 'src/zilch/Env/future.zig')
-rw-r--r--src/zilch/Env/future.zig59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/zilch/Env/future.zig b/src/zilch/Env/future.zig
new file mode 100644
index 0000000..a014990
--- /dev/null
+++ b/src/zilch/Env/future.zig
@@ -0,0 +1,59 @@
+const std = @import("std");
+const Env = @import("root.zig");
+const z = @import("../root.zig");
+
+
+pub fn Future(comptime kind: Env.Value.Kind) type {
+ return struct {
+ const Self = @This();
+
+ future: AnyFuture,
+
+ pub fn fromAny(future: AnyFuture) Self {
+ return .{ .future = future };
+ }
+
+ pub fn await(self: *const Self, env: *Env) !kind.asType() {
+ return self.future.await(env, kind);
+ }
+ };
+}
+
+pub const AnyFuture = union(enum) {
+ const Self = @This();
+
+ result: *Env.Value,
+ err: anyerror,
+
+ pub fn await(self: *const Self, env: *Env, comptime kind: Env.Value.Kind) !kind.asType() {
+ const result = try self.awaitAny(env);
+
+ if (kind != result.kind) {
+ std.log.err("unexpected {}, expected {}", .{ result.kind, kind });
+ @panic("type error");
+ }
+
+ return switch (kind) {
+ .uninit => @panic("uninitialized result"),
+ .string => result.value.string.ptr[0..result.value.string.len],
+ else => @field(result.value, @tagName(kind)),
+ };
+ }
+
+ pub fn awaitAny(self: *const Self, env: *Env) !Env.Value {
+ switch (self.*) {
+ .err => |err| return err,
+ .result => |result| {
+ defer env.pool.destroy(result);
+
+ if (result.kind != .uninit) {
+ return result.*;
+ }
+
+ env.flush();
+
+ return result.*;
+ }
+ }
+ }
+};