aboutsummaryrefslogtreecommitdiff
path: root/src/zilch/Env/future.zig
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2026-10-15 21:57:59 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2026-10-15 21:57:59 +0200
commitdd24e86dad0402917b1713d1afeefaf65c3d1e51 (patch)
tree7f6c9f4d23eb8e60b6b7374233f33423cff40708 /src/zilch/Env/future.zig
parenta1dc55be6e695a1c04e5e9e0f1ce7406a5dd3dff (diff)
zilch: reusing results in processing loop
Diffstat (limited to 'src/zilch/Env/future.zig')
-rw-r--r--src/zilch/Env/future.zig26
1 files changed, 15 insertions, 11 deletions
diff --git a/src/zilch/Env/future.zig b/src/zilch/Env/future.zig
index 6e0cc63..8b88cd8 100644
--- a/src/zilch/Env/future.zig
+++ b/src/zilch/Env/future.zig
@@ -22,10 +22,14 @@ pub fn Future(comptime kind: Env.Value.Kind) type {
pub const AnyFuture = union(enum) {
const Self = @This();
- result: *Env.Value,
+ unresolved: struct {
+ id: usize,
+ value: *Env.Value,
+ },
+ resolved: Env.Value,
err: anyerror,
- pub fn await(self: *const Self, env: *Env, comptime kind: Env.Value.Kind) !kind.asType() {
+ pub fn await(self: *Self, env: *Env, comptime kind: Env.Value.Kind) !kind.asType() {
const result = try self.awaitAny(env);
if (kind != result.kind) {
@@ -34,25 +38,25 @@ pub const AnyFuture = union(enum) {
}
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 {
+ pub fn awaitAny(self: *Self, env: *Env) !Env.Value {
+ defer self.* = undefined;
switch (self.*) {
.err => |err| return err,
- .result => |result| {
- defer env.release(self.*);
-
- if (result.kind != .uninit) {
- return result.*;
- }
+ .resolved => |result| return result,
+ .unresolved => |result| {
+ const value = result.value;
+ defer env.release(value);
env.flush();
- return result.*;
+ self.* = .{ .resolved = value.* };
+
+ return value.*;
}
}
}