gdext icon indicating copy to clipboard operation
gdext copied to clipboard

GDScript -> Rust calls: parameter and return type conversions

Open Bromeon opened this issue 2 years ago • 2 comments

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>

Bromeon avatar Feb 13 '23 20:02 Bromeon

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.

ttencate avatar Feb 14 '23 08:02 ttencate

See also #263.

Bromeon avatar Dec 28 '23 11:12 Bromeon