aboutsummaryrefslogtreecommitdiff
path: root/src/z/Env/Future.zig
blob: 1e28ac733527db3042602433bc9edb961a6d2548 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const std = @import("std");
const Env = @import("root.zig");
const z = @import("../root.zig");

const Self = @This();

result: *Env.Value,

pub fn await(self: *const Self, env: *Env, comptime kind: Env.Value.Kind) kind.asType() {
    const result = 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 {
    defer env.pool.destroy(self.result);

    if (self.result.kind != .uninit) {
        return self.result.*;
    }

    env.flush();

    return self.result.*;
}