diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-10-19 15:14:01 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-10-19 15:16:33 +0200 |
| commit | bbd1364de53186af975ef1694fdc8451208ce626 (patch) | |
| tree | 467e72f70b0fca12fb053618913ee247f7721137 /src/zilch/Env | |
| parent | 5b7b5c48e50d377b794dbbfc1c270ec6fd387e4b (diff) | |
zilch: make object calls type agnostic
Diffstat (limited to 'src/zilch/Env')
| -rw-r--r-- | src/zilch/Env/future.zig | 17 | ||||
| -rw-r--r-- | src/zilch/Env/root.zig | 9 |
2 files changed, 24 insertions, 2 deletions
diff --git a/src/zilch/Env/future.zig b/src/zilch/Env/future.zig index 1b44d85..f829009 100644 --- a/src/zilch/Env/future.zig +++ b/src/zilch/Env/future.zig @@ -13,15 +13,20 @@ pub fn Future(Kind: type) type { return .{ .future = future }; } - pub fn await(self: *const Self, env: *Env) !Kind { + pub fn await(self: *Self, env: *Env) !Kind { return self.future.await(env, Kind); } + + pub fn ignore(self: *Self, env: *Env) void { + return self.future.ignore(env); + } }; } pub const AnyFuture = union(enum) { const Self = @This(); + ignored: void, unresolved: struct { id: usize, value: *Env.Value, @@ -37,6 +42,7 @@ pub const AnyFuture = union(enum) { pub fn awaitAny(self: *Self, env: *Env) !Env.Value { defer self.* = undefined; switch (self.*) { + .ignored => @panic("awaited ignored value"), .err => |err| return err, .resolved => |result| return result, .unresolved => |result| { @@ -51,4 +57,13 @@ pub const AnyFuture = union(enum) { } } } + + pub fn ignore(self: *Self, env: *Env) void { + defer self.* = .ignored; + + switch (self.*) { + .unresolved => env.ignore(self), + else => {}, + } + } }; diff --git a/src/zilch/Env/root.zig b/src/zilch/Env/root.zig index 5244830..d169b5f 100644 --- a/src/zilch/Env/root.zig +++ b/src/zilch/Env/root.zig @@ -12,7 +12,7 @@ pub const Function = @import("Function.zig"); calls: std.ArrayList(Call) = .empty, values: std.ArrayList(Value) = .empty, pool: std.heap.MemoryPool(Value) = .empty, -results: std.ArrayList(*Value) = .empty, +results: std.ArrayList(?*Value) = .empty, gpa: std.mem.Allocator, const Self = @This(); @@ -58,6 +58,13 @@ pub fn release(self: *Self, value: *Value) void { self.pool.destroy(value); } +pub fn ignore(self: *Self, future: *const AnyFuture) void { + if (self.results.items.len < future.unresolved.id and self.results.items[future.unresolved.id] == future.unresolved.value) { + self.pool.destroy(future.unresolved.value); + self.results.items[future.unresolved.id] = null; + } +} + pub fn deinit(self: *Self) void { self.flush(); |