Skip to content

Commit

Permalink
Code organization for export traits
Browse files Browse the repository at this point in the history
  • Loading branch information
Bromeon committed Jul 29, 2023
1 parent acb668b commit 443f25a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 37 deletions.
2 changes: 1 addition & 1 deletion godot-core/src/builtin/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ impl<T: VariantMetadata> Share for Array<T> {

impl<T: VariantMetadata + TypeStringHint> TypeStringHint for Array<T> {
fn type_string() -> String {
format!("{}:{}", sys::VariantType::Array as i32, T::type_string())
format!("{}:{}", VariantType::Array as i32, T::type_string())
}
}

Expand Down
26 changes: 14 additions & 12 deletions godot-core/src/obj/gd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -747,8 +750,7 @@ impl<T: GodotClass> Export for Gd<T> {
}
}

// ----------------------------------------------------------------------------------------------------------------------------------------------
// Trait impls
// Trait impls Property, Export and TypeStringHint for Option<Gd<T>> are covered by blanket impl for Option<T>

impl<T: GodotClass> FromVariant for Gd<T> {
fn try_from_variant(variant: &Variant) -> Result<Self, VariantConversionError> {
Expand Down Expand Up @@ -776,6 +778,16 @@ impl<T: GodotClass> FromVariant for Gd<T> {
}
}

impl<T: GodotClass> FromVariant for Option<Gd<T>> {
fn try_from_variant(variant: &Variant) -> Result<Self, VariantConversionError> {
if variant.is_nil() {
Ok(None)
} else {
Gd::try_from_variant(variant).map(Some)
}
}
}

impl<T: GodotClass> ToVariant for Gd<T> {
fn to_variant(&self) -> Variant {
// The conversion method `object_to_variant` DOES increment the reference-count of the object; so nothing to do here.
Expand Down Expand Up @@ -807,16 +819,6 @@ impl<T: GodotClass> ToVariant for Option<Gd<T>> {
}
}

impl<T: GodotClass> FromVariant for Option<Gd<T>> {
fn try_from_variant(variant: &Variant) -> Result<Self, VariantConversionError> {
if variant.is_nil() {
Ok(None)
} else {
Gd::try_from_variant(variant).map(Some)
}
}
}

impl<T: GodotClass> PartialEq for Gd<T> {
/// ⚠️ Returns whether two `Gd` pointers point to the same object.
///
Expand Down
57 changes: 33 additions & 24 deletions godot-core/src/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<T>

impl<T: TypeStringHint> TypeStringHint for Option<T> {
fn type_string() -> String {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 443f25a

Please sign in to comment.