gdext
gdext copied to clipboard
GDScript -> Rust calls: parameter and return type conversions
We need to work out rules regarding how values from and to Rust #[func]
methods are passed, when invoked from GDScript. Most notably, we should decide which implicit conversions (if at all) we provide. Then, we should test those, and make sure unwanted conversions don't happen (and panic instead).
Generally I'd like an approach where we start quite restrictive, and then can loosen restrictions on a demand basis. This not only avoids bugs and makes things more explicit, but it also prevents performance traps where someone passes Array
to a Vec
, needing reallocation and copying each frame.
Or we keep type-safety, but provide tooling to avoid excessive user-side converting.
Examples:
GDScript | Rust | allowed |
---|---|---|
int |
f32 |
|
float |
i32 |
|
float |
u32 |
|
String |
std::string::String |
|
StringName |
std::string::String |
|
Array |
Array<i32> |
|
Array |
Array<Variant> |
|
Array |
Vec<Variant> |
|
Array[int] |
Array<i32> |
|
Array[int] |
Array<Variant> |
|
... |
Variant |
|
... |
Option<T> |
|
... |
Result<T, E> |
There is prior art in GDNative as well. It lets you receive usize
and u8
at least, which are fallible conversions from the engine's i64
type. Especially usize
is very practical to have for e.g. indices. I haven't checked, but I assume the conversion will panic if the value is out of range.
See also #263.