aboutsummaryrefslogtreecommitdiff
path: root/src/zilch/Env/future.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/zilch/Env/future.zig')
-rw-r--r--src/zilch/Env/future.zig72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/zilch/Env/future.zig b/src/zilch/Env/future.zig
new file mode 100644
index 0000000..43f46c5
--- /dev/null
+++ b/src/zilch/Env/future.zig
@@ -0,0 +1,72 @@
+const std = @import("std");
+const Env = @import("root.zig");
+const z = @import("../root.zig");
+
+
+pub fn Future(Kind: type) type {
+ return struct {
+ const Self = @This();
+
+ future: AnyFuture,
+
+ pub fn fromAny(future: AnyFuture) Self {
+ return .{ .future = future };
+ }
+
+ 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,
+ },
+ resolved: Env.Value,
+ err: anyerror,
+
+ pub fn await(self: *Self, env: *Env, Kind: type) !Kind {
+ const result = try self.awaitAny(env);
+ return result.unwrap(Kind);
+ }
+
+ pub fn awaitAny(self: *Self, env: *Env) !Env.Value {
+ defer self.* = undefined;
+ switch (self.*) {
+ .ignored => {
+ env.flush();
+ return .from({});
+ },
+ .err => |err| return err,
+ .resolved => |result| return result,
+ .unresolved => |result| {
+ const value = result.value;
+ defer env.release(value);
+
+ env.flush();
+
+ self.* = .{ .resolved = value.* };
+
+ return value.*;
+ }
+ }
+ }
+
+ pub fn ignore(self: *Self, env: *Env) void {
+ defer self.* = .ignored;
+
+ switch (self.*) {
+ .unresolved => env.ignore(self),
+ else => {},
+ }
+ }
+};