From dd24e86dad0402917b1713d1afeefaf65c3d1e51 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Thu, 15 Oct 2026 21:57:59 +0200 Subject: zilch: reusing results in processing loop --- src/zilch/Env/future.zig | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'src/zilch/Env/future.zig') 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.*; } } } -- cgit v1.2.3-70-g09d2