-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhermit_type.zig
35 lines (27 loc) · 1017 Bytes
/
hermit_type.zig
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
35
pub fn HermitType(comptime S: type, comptime T: type) type {
return struct {
pub const State = S;
pub const Out = T;
pub const Event = State;
pub const Action = struct {
state: State,
value: ?Out,
pub inline fn _return(state: State, out: Out) Action {
return .{ .state = state, .value = out };
}
pub inline fn _continue(state: State) Action {
return .{ .state = state, .value = null };
}
};
pub const Next = fn (event: Event) Action;
pub const Deinit = fn (state: State) void;
pub fn nop(_: State) void {}
pub inline fn run(state: State, comptime next: Next, comptime deinit: Deinit) Out {
var a = Action._continue(state);
defer deinit(a.state);
const a_i = .always_inline;
while (a.value == null) a = @call(a_i, next, .{a.state});
return a.value.?;
}
};
}