-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprosumer_type.zig
71 lines (59 loc) · 2 KB
/
prosumer_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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
pub fn ProsumerType(comptime S: type, comptime T: type, comptime U: type) type {
return struct {
pub const In = S;
pub const State = T;
pub const Out = U;
pub const EventTag = enum {
_break,
_continue,
_yield,
};
pub const Event = struct {
state: State,
tag: union(EventTag) {
_break: void,
_continue: void,
_yield: In,
},
pub inline fn _break(state: State) Event {
return .{ .state = state, .tag = .{ ._break = {} } };
}
pub inline fn _continue(state: State) Event {
return .{ .state = state, .tag = .{ ._continue = {} } };
}
pub inline fn _yield(in: In, state: State) Event {
return .{ .state = state, .tag = .{ ._yield = in } };
}
};
pub const ActionTag = enum {
_break,
_continue,
_await,
_yield,
};
pub const Action = struct {
state: State,
tag: union(ActionTag) {
_break: void,
_continue: void,
_await: void,
_yield: Out,
},
pub inline fn _break(state: State) Action {
return .{ .state = state, .tag = .{ ._break = {} } };
}
pub inline fn _continue(state: State) Action {
return .{ .state = state, .tag = .{ ._continue = {} } };
}
pub inline fn _await(state: State) Action {
return .{ .state = state, .tag = .{ ._await = {} } };
}
pub inline fn _yield(state: State, out: Out) Action {
return .{ .state = state, .tag = .{ ._yield = out } };
}
};
pub const Next = fn (event: Event) Action;
pub const Deinit = fn (state: State) void;
pub fn nop(_: State) void {}
};
}