diff --git a/godot-core/src/builtin/array.rs b/godot-core/src/builtin/array.rs index ac958bdd8..08bb47b3b 100644 --- a/godot-core/src/builtin/array.rs +++ b/godot-core/src/builtin/array.rs @@ -642,7 +642,7 @@ impl Share for Array { impl TypeStringHint for Array { fn type_string() -> String { - format!("{}:{}", sys::VariantType::Array as i32, T::type_string()) + format!("{}:{}", VariantType::Array as i32, T::type_string()) } } diff --git a/godot-core/src/obj/gd.rs b/godot-core/src/obj/gd.rs index 0bad7581b..325c918f9 100644 --- a/godot-core/src/obj/gd.rs +++ b/godot-core/src/obj/gd.rs @@ -655,6 +655,9 @@ pub unsafe fn raw_object_init( object_ptr } +// ---------------------------------------------------------------------------------------------------------------------------------------------- +// Trait impls + /// Destructor with semantics depending on memory strategy. /// /// * If this `Gd` smart pointer holds a reference-counted type, this will decrement the reference counter. @@ -747,8 +750,7 @@ impl Export for Gd { } } -// ---------------------------------------------------------------------------------------------------------------------------------------------- -// Trait impls +// Trait impls Property, Export and TypeStringHint for Option> are covered by blanket impl for Option impl FromVariant for Gd { fn try_from_variant(variant: &Variant) -> Result { @@ -776,6 +778,16 @@ impl FromVariant for Gd { } } +impl FromVariant for Option> { + fn try_from_variant(variant: &Variant) -> Result { + if variant.is_nil() { + Ok(None) + } else { + Gd::try_from_variant(variant).map(Some) + } + } +} + impl ToVariant for Gd { fn to_variant(&self) -> Variant { // The conversion method `object_to_variant` DOES increment the reference-count of the object; so nothing to do here. @@ -807,16 +819,6 @@ impl ToVariant for Option> { } } -impl FromVariant for Option> { - fn try_from_variant(variant: &Variant) -> Result { - if variant.is_nil() { - Ok(None) - } else { - Gd::try_from_variant(variant).map(Some) - } - } -} - impl PartialEq for Gd { /// ⚠️ Returns whether two `Gd` pointers point to the same object. /// diff --git a/godot-core/src/property.rs b/godot-core/src/property.rs index c18e4ca0d..f97ebd368 100644 --- a/godot-core/src/property.rs +++ b/godot-core/src/property.rs @@ -7,6 +7,9 @@ use crate::builtin::GodotString; use crate::engine::global::PropertyHint; +// ---------------------------------------------------------------------------------------------------------------------------------------------- +// Trait definitions + /// Trait implemented for types that can be used as `#[var]` fields. This creates a copy of the /// value, for some type-specific definition of "copy". For example, `Array`, `Dictionary` and `Gd` are /// returned via `Share::share()` instead of copying the actual data. @@ -23,23 +26,19 @@ pub trait Export: Property { fn default_export_info() -> ExportInfo; } -/// Info needed for godot to understand how to export a type to the editor. -#[derive(Clone, Eq, PartialEq, Debug)] -pub struct ExportInfo { - pub hint: PropertyHint, - pub hint_string: GodotString, +/// Trait for types that can be represented as a type string for use with +/// [`PropertyHint::PROPERTY_HINT_TYPE_STRING`]. +pub trait TypeStringHint { + /// Returns the representation of this type as a type string. + /// + /// See [`PropertyHint.PROPERTY_HINT_TYPE_STRING`]( + /// https://docs.godotengine.org/en/stable/classes/class_%40globalscope.html#enum-globalscope-propertyhint + /// ). + fn type_string() -> String; } -impl ExportInfo { - /// Create a new `ExportInfo` with a property hint of - /// [`PROPERTY_HINT_NONE`](PropertyHint::PROPERTY_HINT_NONE). - pub fn with_hint_none() -> Self { - Self { - hint: PropertyHint::PROPERTY_HINT_NONE, - hint_string: GodotString::new(), - } - } -} +// ---------------------------------------------------------------------------------------------------------------------------------------------- +// Blanket impls for Option impl TypeStringHint for Option { fn type_string() -> String { @@ -80,15 +79,25 @@ where } } -/// Trait for types that can be represented as a type string for use with -/// [`PropertyHint::PROPERTY_HINT_TYPE_STRING`]. -pub trait TypeStringHint { - /// Returns the representation of this type as a type string. - /// - /// See [`PropertyHint.PROPERTY_HINT_TYPE_STRING`]( - /// https://docs.godotengine.org/en/stable/classes/class_%40globalscope.html#enum-globalscope-propertyhint - /// ). - fn type_string() -> String; +// ---------------------------------------------------------------------------------------------------------------------------------------------- +// Export machinery + +/// Info needed for godot to understand how to export a type to the editor. +#[derive(Clone, Eq, PartialEq, Debug)] +pub struct ExportInfo { + pub hint: PropertyHint, + pub hint_string: GodotString, +} + +impl ExportInfo { + /// Create a new `ExportInfo` with a property hint of + /// [`PROPERTY_HINT_NONE`](PropertyHint::PROPERTY_HINT_NONE). + pub fn with_hint_none() -> Self { + Self { + hint: PropertyHint::PROPERTY_HINT_NONE, + hint_string: GodotString::new(), + } + } } /// To export properties to Godot, you must have an impl-block with the `#[godot_api]` attribute, even if