Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions godot-codegen/src/generator/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,16 @@ pub fn make_enum_definition_with(
let index_enum_impl = make_enum_index_impl(enum_);
let bitwise_impls = make_enum_bitwise_operators(enum_, enum_bitmask.as_ref());

let var_trait_set_property = if enum_.is_exhaustive {
let var_trait_set = if enum_.is_exhaustive {
quote! {
fn set_property(&mut self, value: Self::Via) {
*self = <Self as #engine_trait>::from_ord(value);
fn var_set(field: &mut Self, value: Self::Via) {
*field = <Self as #engine_trait>::from_ord(value);
}
}
} else {
quote! {
fn set_property(&mut self, value: Self::Via) {
self.ord = value;
fn var_set(field: &mut Self, value: Self::Via) {
field.ord = value;
}
}
};
Expand Down Expand Up @@ -156,14 +156,24 @@ pub fn make_enum_definition_with(
}

impl crate::registry::property::Var for #name {
fn get_property(&self) -> Self::Via{
<Self as #engine_trait>::ord(*self)
type PubType = Self;

fn var_get(field: &Self) -> Self::Via {
<Self as #engine_trait>::ord(*field)
}

#var_trait_set_property
#var_trait_set

fn var_pub_get(field: &Self) -> Self::PubType {
*field
}

fn var_pub_set(field: &mut Self, value: Self::PubType) {
*field = value;
}

fn var_hint() -> crate::meta::PropertyHintInfo{
crate::meta::PropertyHintInfo{
fn var_hint() -> crate::meta::PropertyHintInfo {
crate::meta::PropertyHintInfo {
hint: #property_hint,
hint_string: crate::builtin::GString::from(#enum_hint_string),
}
Expand Down
3 changes: 3 additions & 0 deletions godot-core/src/builtin/collections/any_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::meta::error::ConvertError;
use crate::meta::{
ArrayElement, ElementType, FromGodot, GodotConvert, GodotFfiVariant, GodotType, ToGodot,
};
use crate::registry::property::SimpleVar;

/// Covariant `Array` that can be either typed or untyped.
///
Expand Down Expand Up @@ -511,6 +512,8 @@ impl FromGodot for AnyArray {
}
}

impl SimpleVar for AnyArray {}

impl fmt::Debug for AnyArray {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.array.fmt(f)
Expand Down
18 changes: 14 additions & 4 deletions godot-core/src/builtin/collections/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1205,12 +1205,22 @@ impl<T: ArrayElement> Clone for Array<T> {
}

impl<T: ArrayElement> Var for Array<T> {
fn get_property(&self) -> Self::Via {
self.to_godot_owned()
type PubType = Self;

fn var_get(field: &Self) -> Self::Via {
field.to_godot_owned()
}

fn var_set(field: &mut Self, value: Self::Via) {
*field = FromGodot::from_godot(value);
}

fn var_pub_get(field: &Self) -> Self::PubType {
field.clone()
}

fn set_property(&mut self, value: Self::Via) {
*self = FromGodot::from_godot(value)
fn var_pub_set(field: &mut Self, value: Self::PubType) {
*field = value;
}

fn var_hint() -> PropertyHintInfo {
Expand Down
12 changes: 2 additions & 10 deletions godot-core/src/builtin/collections/packed_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::meta;
use crate::meta::signed_range::SignedRange;
use crate::meta::{AsArg, FromGodot, GodotConvert, PackedArrayElement, ToGodot};
use crate::obj::EngineEnum;
use crate::registry::property::{Export, Var};
use crate::registry::property::{Export, SimpleVar};

// Many builtin types don't have a #[repr] themselves, but they are used in packed arrays, which assumes certain size and alignment.
// This is mostly a problem for as_slice(), which reinterprets the FFI representation into the "frontend" type like GString.
Expand Down Expand Up @@ -601,15 +601,7 @@ impl<T: PackedArrayElement> ops::IndexMut<usize> for PackedArray<T> {
// ----------------------------------------------------------------------------------------------------------------------------------------------
// Property trait impls

impl<T: PackedArrayElement> Var for PackedArray<T> {
fn get_property(&self) -> Self::Via {
ToGodot::to_godot_owned(self)
}

fn set_property(&mut self, value: Self::Via) {
*self = FromGodot::from_godot(value);
}
}
impl<T: PackedArrayElement> SimpleVar for PackedArray<T> {}

impl<T: PackedArrayElement> Export for PackedArray<T> {
fn export_hint() -> meta::PropertyHintInfo {
Expand Down
42 changes: 32 additions & 10 deletions godot-core/src/obj/dyn_gd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,13 +658,23 @@ where
T: GodotClass,
D: ?Sized + 'static,
{
fn get_property(&self) -> Self::Via {
self.obj.get_property()
type PubType = Self;

fn var_get(field: &Self) -> Self::Via {
<Gd<T> as Var>::var_get(&field.obj)
}

fn var_set(field: &mut Self, value: Self::Via) {
// `var_set` can't be delegated to Gd<T>, since we have to set `erased_obj` as well.
*field = <Self as FromGodot>::from_godot(value);
}

fn var_pub_get(field: &Self) -> Self::PubType {
field.clone()
}

fn set_property(&mut self, value: Self::Via) {
// `set_property` can't be delegated to Gd<T>, since we have to set `erased_obj` as well.
*self = <Self as FromGodot>::from_godot(value);
fn var_pub_set(field: &mut Self, value: Self::PubType) {
*field = value;
}
}

Expand Down Expand Up @@ -707,13 +717,25 @@ where
T: GodotClass,
D: ?Sized + 'static,
{
fn get_property(&self) -> Self::Via {
Self::get_property_inner(self)
// Not Option<...> -- accessing from Rust through Var trait should not expose larger API than OnEditor itself.
type PubType = <DynGd<T, D> as GodotConvert>::Via;

fn var_get(field: &Self) -> Self::Via {
Self::get_property_inner(field)
}

fn var_set(field: &mut Self, value: Self::Via) {
// `var_set` can't be delegated to Gd<T>, since we have to set `erased_obj` as well.
Self::set_property_inner(field, value);
}

fn var_pub_get(field: &Self) -> Self::PubType {
Self::var_get(field)
.expect("generated #[var(pub)] getter: uninitialized OnEditor<DynGd<T, D>>")
}

fn set_property(&mut self, value: Self::Via) {
// `set_property` can't be delegated to Gd<T>, since we have to set `erased_obj` as well.
Self::set_property_inner(self, value)
fn var_pub_set(field: &mut Self, value: Self::PubType) {
Self::var_set(field, Some(value))
}
}

Expand Down
31 changes: 17 additions & 14 deletions godot-core/src/obj/gd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::obj::{
};
use crate::private::{callbacks, PanicPayload};
use crate::registry::class::try_dynify_object;
use crate::registry::property::{object_export_element_type_string, Export, Var};
use crate::registry::property::{object_export_element_type_string, Export, SimpleVar, Var};
use crate::{classes, meta, out};

/// Smart pointer to objects owned by the Godot engine.
Expand Down Expand Up @@ -1045,15 +1045,7 @@ impl<T: GodotClass> Clone for Gd<T> {
}
}

impl<T: GodotClass> Var for Gd<T> {
fn get_property(&self) -> Self::Via {
self.to_godot_owned()
}

fn set_property(&mut self, value: Self::Via) {
*self = FromGodot::from_godot(value)
}
}
impl<T: GodotClass> SimpleVar for Gd<T> {}

/// See [`Gd` Exporting](struct.Gd.html#exporting) section.
impl<T> Export for Option<Gd<T>>
Expand Down Expand Up @@ -1089,12 +1081,23 @@ impl<T> Var for OnEditor<Gd<T>>
where
T: GodotClass,
{
fn get_property(&self) -> Self::Via {
Self::get_property_inner(self)
// Not Option<...> -- accessing from Rust through Var trait should not expose larger API than OnEditor itself.
type PubType = <Gd<T> as GodotConvert>::Via;

fn var_get(field: &Self) -> Self::Via {
Self::get_property_inner(field)
}

fn var_set(field: &mut Self, value: Self::Via) {
Self::set_property_inner(field, value);
}

fn var_pub_get(field: &Self) -> Self::PubType {
Self::var_get(field).expect("generated #[var(pub)] getter: uninitialized OnEditor<Gd<T>>")
}

fn set_property(&mut self, value: Self::Via) {
Self::set_property_inner(self, value)
fn var_pub_set(field: &mut Self, value: Self::PubType) {
Self::var_set(field, Some(value))
}
}

Expand Down
3 changes: 3 additions & 0 deletions godot-core/src/obj/instance_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::num::NonZeroU64;

use crate::meta::error::{ConvertError, FromGodotError};
use crate::meta::{FromGodot, GodotConvert, ToGodot};
use crate::registry::property::SimpleVar;

/// Represents a non-zero instance ID.
///
Expand Down Expand Up @@ -107,3 +108,5 @@ impl FromGodot for InstanceId {
Self::try_from_i64(via).ok_or_else(|| FromGodotError::ZeroInstanceId.into_error(via))
}
}

impl SimpleVar for InstanceId {}
Loading
Loading