Consolidated API to pass custom types over FFI boundary · Issue #414 · godot-rust/gdext (original) (raw)

This issue is similar to #227 and perhaps also related to @lilizoey's comment on improving error messages. So I'm not quite sure if the necessary features are already all there, and I'm just failing to connect the dots.

I'm wondering how to support custom primitive data types in function signatures. For instance:

// Example 1

#[derive(Debug, Clone, Copy, PartialEq, FromVariant, ToVariant)] enum EventKind { DrawStart, Drag, DragEnd, }

#[godot_api] impl CustomProgressBar { #[signal] fn progress_bar_change(progress: i64, event_kind: EventKind); }

// Example 2

#[godot_api] impl CustomItemList { #[signal] fn item_selected(index: Option); }

In these examples the compiler complains:

My work-around is typically to move the encoding/decoding into the user code, for instance:

#[godot_api] impl CustomProgressBar { #[signal] fn progress_bar_change(progress: i64, event_kind: Variant); }

But now this signature is kind of vague, and every implementator of the signal must remember how to actually decode that variant.

In both cases it would be great if I could specify a kind of encoding/decoding such that e.g. the Option<u32> encodes the None as -1 etc.

I'd assume that the FromVariant / ToVariant traits solve this problem partially, but somehow even in the case where the custom type implements these traits, it is not possible to use it directly. The error message also mentions VariantMetadata is required to be implemented as well, but according to this comment, it is probably not a trait the user should implement manually?