-
Notifications
You must be signed in to change notification settings - Fork 208
/
func_state.rs
178 lines (157 loc) · 5.1 KB
/
func_state.rs
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use gdnative_bindings::Reference;
use gdnative_core::core_types::{ToVariant, Variant, VariantType};
use gdnative_core::export::user_data::{LocalCellData, Map, MapMut};
use gdnative_core::export::{
ClassBuilder, ExportInfo, PropertyUsage, Signal, SignalArgument, StaticArgs, StaticArgsMethod,
};
use gdnative_core::export::{NativeClass, NativeClassMethods};
use gdnative_core::godot_site;
use gdnative_core::object::ownership::{Shared, Unique};
use gdnative_core::object::{Instance, RefInstance};
use gdnative_derive::FromVarargs;
use crate::future::Resume;
pub(crate) struct FuncState {
kind: Kind,
}
enum Kind {
Resolved(Variant),
Resumable(Resume<Variant>),
Pending,
}
impl NativeClass for FuncState {
type Base = Reference;
type UserData = LocalCellData<FuncState>;
fn class_name() -> &'static str {
// Sort of just praying that there will be no duplicates of this.
"__GDNATIVE_ASYNC_INTERNAL__FuncState"
}
fn register_properties(builder: &ClassBuilder<Self>) {
builder.add_signal(Signal {
name: "completed",
args: &[SignalArgument {
name: "value",
default: Variant::new(),
export_info: ExportInfo::new(VariantType::Nil),
usage: PropertyUsage::DEFAULT,
}],
});
builder.add_signal(Signal {
name: "resumable",
args: &[],
});
}
}
impl FuncState {
pub fn new() -> Instance<Self, Unique> {
Instance::emplace(FuncState {
kind: Kind::Pending,
})
}
}
pub(super) fn resolve(this: RefInstance<'_, FuncState, Shared>, value: Variant) {
this.script()
.map_mut(|s| {
match s.kind {
Kind::Resolved(_) => {
panic!("`resolve` should only be called once for each FuncState")
}
Kind::Pending => {}
Kind::Resumable(_) => {
gdnative_core::log::warn(
Default::default(),
"async function resolved while waiting for a `resume` call",
);
}
}
s.kind = Kind::Resolved(value.clone());
})
.expect("no reentrancy");
this.base().emit_signal("completed", &[value]);
}
pub(super) fn make_resumable(this: RefInstance<'_, FuncState, Shared>, resume: Resume<Variant>) {
let kind = this
.script()
.map_mut(|s| std::mem::replace(&mut s.kind, Kind::Resumable(resume)))
.expect("no reentrancy");
match kind {
Kind::Resolved(_) => {
panic!("`make_resumable` should not be called after resolution")
}
Kind::Resumable(_) => {
gdnative_core::log::warn(
Default::default(),
"`make_resumable` called when there is a previous pending future",
);
}
Kind::Pending => {
this.base().emit_signal("resumable", &[]);
}
}
}
#[derive(Clone, Copy, Debug, Default)]
struct IsValidFn;
#[derive(FromVarargs)]
struct IsValidArgs {
#[opt]
extended_check: Option<bool>,
}
impl StaticArgsMethod<FuncState> for IsValidFn {
type Args = IsValidArgs;
fn call(&self, this: RefInstance<'_, FuncState, Shared>, args: Self::Args) -> Variant {
if args.extended_check.is_some() {
gdnative_core::log::warn(
Self::site().unwrap(),
"`extended_check` is set, but it has no effect on Rust function state objects",
)
}
this.script()
.map(|s| match &s.kind {
Kind::Resumable(_) => true,
Kind::Resolved(_) | Kind::Pending => false,
})
.unwrap()
.to_variant()
}
fn site() -> Option<gdnative_core::log::Site<'static>> {
Some(godot_site!(FunctionState::is_valid))
}
}
#[derive(Clone, Copy, Debug, Default)]
struct ResumeFn;
#[derive(FromVarargs)]
struct ResumeArgs {
#[opt]
arg: Variant,
}
impl StaticArgsMethod<FuncState> for ResumeFn {
type Args = ResumeArgs;
fn call(&self, this: RefInstance<'_, FuncState, Shared>, args: Self::Args) -> Variant {
this.map_mut(
|s, owner| match std::mem::replace(&mut s.kind, Kind::Pending) {
Kind::Resumable(resume) => {
resume.resume(args.arg);
owner.to_variant()
}
Kind::Pending => owner.to_variant(),
Kind::Resolved(result) => {
s.kind = Kind::Resolved(result.clone());
result
}
},
)
.expect("no reentrancy")
}
fn site() -> Option<gdnative_core::log::Site<'static>> {
Some(godot_site!(FunctionState::is_valid))
}
}
impl NativeClassMethods for FuncState {
fn register(builder: &ClassBuilder<Self>) {
builder
.build_method("is_valid", StaticArgs::new(IsValidFn))
.done_stateless();
builder
.build_method("resume", StaticArgs::new(ResumeFn))
.done_stateless();
}
}