diff --git a/examples/dodge-the-creeps/rust/src/main_scene.rs b/examples/dodge-the-creeps/rust/src/main_scene.rs index 901cb3790..5b3c54461 100644 --- a/examples/dodge-the-creeps/rust/src/main_scene.rs +++ b/examples/dodge-the-creeps/rust/src/main_scene.rs @@ -91,7 +91,7 @@ impl Main { mob_scene.set_rotation(direction); - self.base.add_child(mob_scene.share().upcast()); + self.base.add_child(mob_scene.clone().upcast()); let mut mob = mob_scene.cast::(); let range = { diff --git a/godot-core/src/builtin/array.rs b/godot-core/src/builtin/array.rs index 619e3a1d5..f3a273a13 100644 --- a/godot-core/src/builtin/array.rs +++ b/godot-core/src/builtin/array.rs @@ -34,9 +34,9 @@ use sys::{ffi_methods, interface_fn, GodotFfi}; /// Like in GDScript, `Array` acts as a reference type: multiple `Array` instances may /// refer to the same underlying array, and changes to one are visible in the other. /// -/// To create a copy that shares data with the original array, use [`Share::share()`]. If you want -/// to create a copy of the data, use [`duplicate_shallow()`][Self::duplicate_shallow] or -/// [`duplicate_deep()`][Self::duplicate_deep]. +/// To create a copy that shares data with the original array, use [`Clone::clone()`]. +/// If you want to create a copy of the data, use [`duplicate_shallow()`][Self::duplicate_shallow] +/// or [`duplicate_deep()`][Self::duplicate_deep]. /// /// # Thread safety /// @@ -245,7 +245,7 @@ impl Array { /// (such as `Array`, `Dictionary` and `Object`) will still refer to the same value. /// /// To create a deep copy, use [`duplicate_deep()`][Self::duplicate_deep] instead. - /// To create a new reference to the same array data, use [`share()`][Share::share]. + /// To create a new reference to the same array data, use [`clone()`][Clone::clone]. pub fn duplicate_shallow(&self) -> Self { let duplicate: VariantArray = self.as_inner().duplicate(false); // SAFETY: duplicate() returns a typed array with the same type as Self @@ -257,7 +257,7 @@ impl Array { /// still be shallow copied. /// /// To create a shallow copy, use [`duplicate_shallow()`][Self::duplicate_shallow] instead. - /// To create a new reference to the same array data, use [`share()`][Share::share]. + /// To create a new reference to the same array data, use [`clone()`][Clone::clone]. pub fn duplicate_deep(&self) -> Self { let duplicate: VariantArray = self.as_inner().duplicate(true); // SAFETY: duplicate() returns a typed array with the same type as Self @@ -592,7 +592,7 @@ impl Array { // // - `from_arg_ptr` // Arrays are properly initialized through a `from_sys` call, but the ref-count should be incremented -// as that is the callee's responsibility. Which we do by calling `std::mem::forget(array.share())`. +// as that is the callee's responsibility. Which we do by calling `std::mem::forget(array.clone())`. unsafe impl GodotFfi for Array { ffi_methods! { type sys::GDExtensionTypePtr = *mut Opaque; fn from_sys; @@ -609,7 +609,7 @@ unsafe impl GodotFfi for Array { unsafe fn from_arg_ptr(ptr: sys::GDExtensionTypePtr, _call_type: sys::PtrcallType) -> Self { let array = Self::from_sys(ptr); - std::mem::forget(array.share()); + std::mem::forget(array.clone()); array } } @@ -624,10 +624,11 @@ impl fmt::Debug for Array { /// Creates a new reference to the data in this array. Changes to the original array will be /// reflected in the copy and vice versa. /// -/// To create a (mostly) independent copy instead, see [`VariantArray::duplicate_shallow()`] and -/// [`VariantArray::duplicate_deep()`]. -impl Share for Array { - fn share(&self) -> Self { +/// To create a (mostly) independent copy instead, see [`Array::duplicate_shallow()`] and +/// [`Array::duplicate_deep()`]. +impl Clone for Array { + fn clone(&self) -> Self { + // SAFETY: `self` is a valid array, since we have a reference that keeps it alive. let array = unsafe { Self::from_sys_init(|self_ptr| { let ctor = ::godot_ffi::builtin_fn!(array_construct_copy); @@ -635,12 +636,19 @@ impl Share for Array { ctor(self_ptr, args.as_ptr()); }) }; + array .with_checked_type() .expect("copied array should have same type as original array") } } +impl Share for Array { + fn share(&self) -> Self { + self.clone() + } +} + impl TypeStringHint for Array { fn type_string() -> String { format!("{}:{}", VariantType::Array as i32, T::type_string()) @@ -651,7 +659,7 @@ impl Property for Array { type Intermediate = Self; fn get_property(&self) -> Self::Intermediate { - self.share() + self.clone() } fn set_property(&mut self, value: Self::Intermediate) { diff --git a/godot-core/src/builtin/dictionary.rs b/godot-core/src/builtin/dictionary.rs index fab6723a4..1418ad2af 100644 --- a/godot-core/src/builtin/dictionary.rs +++ b/godot-core/src/builtin/dictionary.rs @@ -49,8 +49,8 @@ impl Dictionary { /// will not be shared with the original dictionary. Note that any `Object`-derived elements will /// still be shallow copied. /// - /// To create a shallow copy, use [`Self::duplicate_shallow`] instead. To create a new reference to - /// the same array data, use [`Share::share`]. + /// To create a shallow copy, use [`Self::duplicate_shallow()`] instead. + /// To create a new reference to the same dictionary data, use [`clone()`][Clone::clone]. /// /// _Godot equivalent: `dict.duplicate(true)`_ pub fn duplicate_deep(&self) -> Self { @@ -61,8 +61,8 @@ impl Dictionary { /// any reference types (such as `Array`, `Dictionary` and `Object`) will still refer to the /// same value. /// - /// To create a deep copy, use [`Self::duplicate_deep`] instead. To create a new reference to the - /// same dictionary data, use [`Share::share`]. + /// To create a deep copy, use [`Self::duplicate_deep()`] instead. + /// To create a new reference to the same dictionary data, use [`clone()`][Clone::clone]. /// /// _Godot equivalent: `dict.duplicate(false)`_ pub fn duplicate_shallow(&self) -> Self { @@ -263,7 +263,7 @@ impl Dictionary { // - `from_arg_ptr` // Dictionaries are properly initialized through a `from_sys` call, but the ref-count should be // incremented as that is the callee's responsibility. Which we do by calling -// `std::mem::forget(dictionary.share())`. +// `std::mem::forget(dictionary.clone())`. unsafe impl GodotFfi for Dictionary { ffi_methods! { type sys::GDExtensionTypePtr = *mut Opaque; fn from_sys; @@ -280,7 +280,7 @@ unsafe impl GodotFfi for Dictionary { unsafe fn from_arg_ptr(ptr: sys::GDExtensionTypePtr, _call_type: sys::PtrcallType) -> Self { let dictionary = Self::from_sys(ptr); - std::mem::forget(dictionary.share()); + std::mem::forget(dictionary.clone()); dictionary } } @@ -304,8 +304,9 @@ impl fmt::Debug for Dictionary { /// /// To create a (mostly) independent copy instead, see [`Dictionary::duplicate_shallow()`] and /// [`Dictionary::duplicate_deep()`]. -impl Share for Dictionary { - fn share(&self) -> Self { +impl Clone for Dictionary { + fn clone(&self) -> Self { + // SAFETY: `self` is a valid dictionary, since we have a reference that keeps it alive. unsafe { Self::from_sys_init(|self_ptr| { let ctor = sys::builtin_fn!(dictionary_construct_copy); @@ -316,11 +317,17 @@ impl Share for Dictionary { } } +impl Share for Dictionary { + fn share(&self) -> Self { + self.clone() + } +} + impl Property for Dictionary { type Intermediate = Self; fn get_property(&self) -> Self::Intermediate { - self.share() + self.clone() } fn set_property(&mut self, value: Self::Intermediate) { diff --git a/godot-core/src/builtin/string/godot_string.rs b/godot-core/src/builtin/string/godot_string.rs index aa581039b..2bc979973 100644 --- a/godot-core/src/builtin/string/godot_string.rs +++ b/godot-core/src/builtin/string/godot_string.rs @@ -108,7 +108,7 @@ impl GodotString { // - `from_arg_ptr` // Strings are properly initialized through a `from_sys` call, but the ref-count should be // incremented as that is the callee's responsibility. Which we do by calling -// `std::mem::forget(string.share())`. +// `std::mem::forget(string.clone())`. unsafe impl GodotFfi for GodotString { ffi_methods! { type sys::GDExtensionTypePtr = *mut Opaque; fn from_sys; diff --git a/godot-core/src/builtin/string/node_path.rs b/godot-core/src/builtin/string/node_path.rs index a88e5e623..270ed43cc 100644 --- a/godot-core/src/builtin/string/node_path.rs +++ b/godot-core/src/builtin/string/node_path.rs @@ -46,7 +46,7 @@ impl NodePath { // - `from_arg_ptr` // NodePaths are properly initialized through a `from_sys` call, but the ref-count should be // incremented as that is the callee's responsibility. Which we do by calling -// `std::mem::forget(node_path.share())`. +// `std::mem::forget(node_path.clone())`. unsafe impl GodotFfi for NodePath { ffi_methods! { type sys::GDExtensionTypePtr = *mut Opaque; fn from_sys; diff --git a/godot-core/src/builtin/string/string_name.rs b/godot-core/src/builtin/string/string_name.rs index 8d65c5b53..e652ec1df 100644 --- a/godot-core/src/builtin/string/string_name.rs +++ b/godot-core/src/builtin/string/string_name.rs @@ -73,7 +73,7 @@ impl StringName { // - `from_arg_ptr` // StringNames are properly initialized through a `from_sys` call, but the ref-count should be // incremented as that is the callee's responsibility. Which we do by calling -// `std::mem::forget(string_name.share())`. +// `std::mem::forget(string_name.clone())`. unsafe impl GodotFfi for StringName { ffi_methods! { type sys::GDExtensionTypePtr = *mut Opaque; fn from_sys; diff --git a/godot-core/src/engine.rs b/godot-core/src/engine.rs index b4f8c355a..3160beb62 100644 --- a/godot-core/src/engine.rs +++ b/godot-core/src/engine.rs @@ -112,10 +112,8 @@ where // This would need more sophisticated upcast design, e.g. T::upcast_{ref|mut}::() for indirect relations // to make the indirect Deref more explicit - use crate::obj::Share; - let path = path.into(); - let node = self.share().upcast::(); + let node = self.clone().upcast::(); ::try_get_node_as(&*node, path) } diff --git a/godot-core/src/obj/gd.rs b/godot-core/src/obj/gd.rs index 85f332b78..1d9bdbd01 100644 --- a/godot-core/src/obj/gd.rs +++ b/godot-core/src/obj/gd.rs @@ -39,7 +39,7 @@ use crate::{callbacks, engine, out}; /// In particular, the memory management strategy is fully dependent on `T`: /// /// * Objects of type [`RefCounted`] or inherited from it are **reference-counted**. This means that every time a smart pointer is -/// shared using [`Share::share()`], the reference counter is incremented, and every time one is dropped, it is decremented. +/// shared using [`Clone::clone()`], the reference counter is incremented, and every time one is dropped, it is decremented. /// This ensures that the last reference (either in Rust or Godot) will deallocate the object and call `T`'s destructor. /// /// * Objects inheriting from [`Object`] which are not `RefCounted` (or inherited) are **manually-managed**. @@ -309,7 +309,7 @@ impl Gd { /// struct MyClass {} /// /// let obj: Gd = Gd::new_default(); - /// let base = obj.share().upcast::(); + /// let base = obj.clone().upcast::(); /// ``` pub fn upcast(self) -> Gd where @@ -462,7 +462,7 @@ impl Gd { /// Returns a callable referencing a method from this object named `method_name`. pub fn callable>(&self, method_name: S) -> Callable { - Callable::from_object_method(self.share(), method_name) + Callable::from_object_method(self.clone(), method_name) } } @@ -590,7 +590,7 @@ where if T::Mem::pass_as_ref(call_type) { interface_fn!(ref_set_object)(ptr as sys::GDExtensionRefPtr, self.obj_sys()) } else { - std::ptr::write(ptr as *mut _, self.opaque) + ptr::write(ptr as *mut _, self.opaque) } // We've passed ownership to caller. std::mem::forget(self); @@ -709,10 +709,16 @@ impl Drop for Gd { } } +impl Clone for Gd { + fn clone(&self) -> Self { + out!("Gd::clone"); + Self::from_opaque(self.opaque).with_inc_refcount() + } +} + impl Share for Gd { fn share(&self) -> Self { - out!("Gd::share"); - Self::from_opaque(self.opaque).with_inc_refcount() + self.clone() } } @@ -739,7 +745,7 @@ impl Property for Gd { type Intermediate = Self; fn get_property(&self) -> Self { - self.share() + self.clone() } fn set_property(&mut self, value: Self) { diff --git a/godot-core/src/obj/traits.rs b/godot-core/src/obj/traits.rs index 2f0a5d1a5..abca5c329 100644 --- a/godot-core/src/obj/traits.rs +++ b/godot-core/src/obj/traits.rs @@ -71,6 +71,7 @@ pub trait Share { /// Creates a new reference that points to the same object. /// /// If the referred-to object is reference-counted, this will increment the count. + #[deprecated = "Replaced with `Clone::clone()`."] fn share(&self) -> Self; } diff --git a/godot/src/lib.rs b/godot/src/lib.rs index 2aa5ecc6a..23c0ec5a9 100644 --- a/godot/src/lib.rs +++ b/godot/src/lib.rs @@ -12,7 +12,13 @@ //! //! Godot is written in C++, which doesn't have the same strict guarantees about safety and //! mutability that Rust does. As a result, not everything in this crate will look and feel -//! entirely "rusty". We distinguish four different kinds of types: +//! entirely "rusty". +//! +//! Traits such as `Clone`, `PartialEq` or `PartialOrd` are designed to mirror Godot semantics, +//! except in cases where Rust is stricter (e.g. float ordering). Cloning a type results in the +//! same observable behavior as assignment or parameter-passing of a GDScript variable. +//! +//! We distinguish four different kinds of types: //! //! 1. **Value types**: `i64`, `f64`, and mathematical types like //! [`Vector2`][crate::builtin::Vector2] and [`Color`][crate::builtin::Color]. @@ -43,11 +49,9 @@ //! careful when using such types. For example, when iterating over an `Array`, make sure that //! it isn't being modified at the same time through another reference. //! -//! To avoid confusion these types don't implement `Clone`. You can use -//! [`Share`][crate::obj::Share] to create a new reference to the same instance, and -//! type-specific methods such as -//! [`Array::duplicate_deep()`][crate::builtin::Array::duplicate_deep] to make actual -//! copies.

+//! `Clone::clone()` on these types creates a new reference to the same instance, while +//! type-specific methods such as [`Array::duplicate_deep()`][crate::builtin::Array::duplicate_deep] +//! can be used to make actual copies.

//! //! 4. **Manually managed types**: [`Gd`][crate::obj::Gd] where `T` inherits from //! [`Object`][crate::engine::Object] but not from [`RefCounted`][crate::engine::RefCounted]; diff --git a/itest/rust/src/builtin_tests/containers/array_test.rs b/itest/rust/src/builtin_tests/containers/array_test.rs index e0bf5e247..242f94651 100644 --- a/itest/rust/src/builtin_tests/containers/array_test.rs +++ b/itest/rust/src/builtin_tests/containers/array_test.rs @@ -99,7 +99,7 @@ fn array_hash() { #[itest] fn array_share() { let mut array = array![1, 2]; - let shared = array.share(); + let shared = array.clone(); array.set(0, 3); assert_eq!(shared.get(0), 3); } @@ -372,7 +372,7 @@ fn untyped_array_return_from_godot_func() { let mut node = Node::new_alloc(); let mut child = Node::new_alloc(); child.set_name("child_node".into()); - node.add_child(child.share()); + node.add_child(child.clone()); node.queue_free(); // Do not leak even if the test fails. let result = node.get_node_and_resource("child_node".into()); @@ -409,7 +409,7 @@ fn typed_array_return_from_godot_func() { let mut node = Node::new_alloc(); let mut child = Node::new_alloc(); child.set_name("child_node".into()); - node.add_child(child.share()); + node.add_child(child.clone()); node.queue_free(); // Do not leak even if the test fails. let children = node.get_children(); @@ -419,7 +419,7 @@ fn typed_array_return_from_godot_func() { #[itest] fn typed_array_try_from_untyped() { let node = Node::new_alloc(); - let array = VariantArray::from(&[node.share().to_variant()]); + let array = VariantArray::from(&[node.clone().to_variant()]); assert_eq!( array.to_variant().try_to::>>>(), Err(VariantConversionError::BadType) @@ -430,7 +430,7 @@ fn typed_array_try_from_untyped() { #[itest] fn untyped_array_try_from_typed() { let node = Node::new_alloc(); - let array = Array::>>::from(&[Some(node.share())]); + let array = Array::>>::from(&[Some(node.clone())]); assert_eq!( array.to_variant().try_to::(), Err(VariantConversionError::BadType) diff --git a/itest/rust/src/builtin_tests/containers/callable_test.rs b/itest/rust/src/builtin_tests/containers/callable_test.rs index fbfa13fac..50f8b59c3 100644 --- a/itest/rust/src/builtin_tests/containers/callable_test.rs +++ b/itest/rust/src/builtin_tests/containers/callable_test.rs @@ -8,7 +8,7 @@ use godot::bind::{godot_api, GodotClass}; use godot::builtin::inner::InnerCallable; use godot::builtin::{varray, Callable, GodotString, StringName, ToVariant, Variant}; use godot::engine::{Node2D, Object}; -use godot::obj::{Gd, Share}; +use godot::obj::Gd; use crate::framework::itest; @@ -66,7 +66,7 @@ fn callable_object_method() { let obj = Gd::::new_default(); let callable = obj.callable("foo"); - assert_eq!(callable.object(), Some(obj.share().upcast::())); + assert_eq!(callable.object(), Some(obj.clone().upcast::())); assert_eq!(callable.object_id(), Some(obj.instance_id())); assert_eq!(callable.method_name(), Some("foo".into())); @@ -109,7 +109,7 @@ fn callable_call_return() { #[itest] fn callable_call_engine() { let obj = Node2D::new_alloc(); - let cb = Callable::from_object_method(obj.share(), "set_position"); + let cb = Callable::from_object_method(obj.clone(), "set_position"); let inner: InnerCallable = cb.as_inner(); assert!(!inner.is_null()); diff --git a/itest/rust/src/builtin_tests/containers/dictionary_test.rs b/itest/rust/src/builtin_tests/containers/dictionary_test.rs index 6765b073b..d80a22b09 100644 --- a/itest/rust/src/builtin_tests/containers/dictionary_test.rs +++ b/itest/rust/src/builtin_tests/containers/dictionary_test.rs @@ -7,7 +7,6 @@ use std::collections::{HashMap, HashSet}; use godot::builtin::{dict, varray, Dictionary, FromVariant, ToVariant, Variant}; -use godot::obj::Share; use crate::framework::{expect_panic, itest}; @@ -94,10 +93,10 @@ fn dictionary_clone() { }; let dictionary = dict! { "foo": 0, - "bar": subdictionary.share() + "bar": subdictionary.clone() }; #[allow(clippy::redundant_clone)] - let clone = dictionary.share(); + let clone = dictionary.clone(); Dictionary::from_variant(&clone.get("bar").unwrap()).insert("final", 4); assert_eq!(subdictionary.get("final"), Some(4.to_variant())); } @@ -120,7 +119,7 @@ fn dictionary_duplicate_deep() { }; let dictionary = dict! { "foo": 0, - "bar": subdictionary.share() + "bar": subdictionary.clone() }; let clone = dictionary.duplicate_deep(); Dictionary::from_variant(&clone.get("bar").unwrap()).insert("baz", 4); @@ -139,7 +138,7 @@ fn dictionary_duplicate_shallow() { }; let dictionary = dict! { "foo": 0, - "bar": subdictionary.share() + "bar": subdictionary.clone() }; let mut clone = dictionary.duplicate_shallow(); Dictionary::from_variant(&clone.get("bar").unwrap()).insert("baz", 4); @@ -374,7 +373,7 @@ fn dictionary_iter_insert() { "baz": "foobar", "nil": Variant::nil(), }; - let mut dictionary2 = dictionary.share(); + let mut dictionary2 = dictionary.clone(); let mut iter = dictionary.iter_shared(); iter.next(); @@ -396,7 +395,7 @@ fn dictionary_iter_insert_after_completion() { "baz": "foobar", "nil": Variant::nil(), }; - let mut dictionary2 = dictionary.share(); + let mut dictionary2 = dictionary.clone(); let mut iter = dictionary.iter_shared(); for _ in 0..4 { iter.next(); @@ -411,7 +410,7 @@ fn dictionary_iter_insert_after_completion() { #[itest] fn dictionary_iter_big() { let dictionary: Dictionary = (0..256).zip(0..256).collect(); - let mut dictionary2 = dictionary.share(); + let mut dictionary2 = dictionary.clone(); let mut iter = dictionary.iter_shared(); for _ in 0..4 { @@ -511,7 +510,7 @@ fn dictionary_iter_clear() { "baz": "foobar", "nil": Variant::nil(), }; - let mut dictionary2 = dictionary.share(); + let mut dictionary2 = dictionary.clone(); let mut iter = dictionary.iter_shared(); iter.next(); @@ -552,7 +551,7 @@ fn dictionary_iter_erase() { "baz": "foobar", "nil": Variant::nil(), }; - let mut dictionary2 = dictionary.share(); + let mut dictionary2 = dictionary.clone(); let mut iter = dictionary.iter_shared(); iter.next(); diff --git a/itest/rust/src/builtin_tests/containers/signal_test.rs b/itest/rust/src/builtin_tests/containers/signal_test.rs index 29971bbfb..77a25304f 100644 --- a/itest/rust/src/builtin_tests/containers/signal_test.rs +++ b/itest/rust/src/builtin_tests/containers/signal_test.rs @@ -10,7 +10,7 @@ use godot::bind::{godot_api, GodotClass}; use godot::builtin::{GodotString, Variant}; use godot::engine::Object; -use godot::obj::{Base, Gd, Share}; +use godot::obj::{Base, Gd}; use godot::sys; use crate::framework::itest; @@ -50,7 +50,7 @@ impl Receiver { } #[func] fn receive_2_arg(&self, arg1: Gd, arg2: GodotString) { - assert_eq!(self.base.share(), arg1); + assert_eq!(self.base.clone(), arg1); assert_eq!(SIGNAL_ARG_STRING, arg2.to_string()); self.used[2].set(true); @@ -69,7 +69,7 @@ fn signals() { vec![], vec![Variant::from(987)], vec![ - Variant::from(receiver.share()), + Variant::from(receiver.clone()), Variant::from(SIGNAL_ARG_STRING), ], ]; diff --git a/itest/rust/src/builtin_tests/containers/variant_test.rs b/itest/rust/src/builtin_tests/containers/variant_test.rs index 57918c2a9..3a9211f06 100644 --- a/itest/rust/src/builtin_tests/containers/variant_test.rs +++ b/itest/rust/src/builtin_tests/containers/variant_test.rs @@ -116,9 +116,8 @@ fn variant_equal() { #[itest] fn variant_call() { - use godot::obj::Share; let node2d = Node2D::new_alloc(); - let variant = Variant::from(node2d.share()); + let variant = Variant::from(node2d.clone()); // Object let position = Vector2::new(4.0, 5.0); diff --git a/itest/rust/src/engine_tests/native_structures_test.rs b/itest/rust/src/engine_tests/native_structures_test.rs index ebe6e9eaf..64c886efc 100644 --- a/itest/rust/src/engine_tests/native_structures_test.rs +++ b/itest/rust/src/engine_tests/native_structures_test.rs @@ -8,7 +8,7 @@ use crate::framework::itest; use godot::engine::native::{AudioFrame, CaretInfo, Glyph}; use godot::engine::text_server::Direction; use godot::engine::{TextServer, TextServerExtension, TextServerExtensionVirtual}; -use godot::prelude::{godot_api, Base, Gd, GodotClass, Rect2, Rid, Share, Variant}; +use godot::prelude::{godot_api, Base, Gd, GodotClass, Rect2, Rid, Variant}; use std::cell::Cell; @@ -94,7 +94,7 @@ fn test_native_structure_out_parameter() { // function which uses an 'out' pointer parameter. let mut ext: Gd = Gd::new_default(); let result = ext - .share() + .clone() .upcast::() .shaped_text_get_carets(Rid::new(100), 200); @@ -127,7 +127,7 @@ fn test_native_structure_pointer_to_array_parameter() { // Instantiate a TextServerExtension. let ext: Gd = Gd::new_default(); let result = ext - .share() + .clone() .upcast::() .shaped_text_get_glyphs(Rid::new(100)); diff --git a/itest/rust/src/engine_tests/node_test.rs b/itest/rust/src/engine_tests/node_test.rs index d14e205ae..29b95fb43 100644 --- a/itest/rust/src/engine_tests/node_test.rs +++ b/itest/rust/src/engine_tests/node_test.rs @@ -8,7 +8,6 @@ use std::str::FromStr; use godot::builtin::{NodePath, Variant}; use godot::engine::{global, Node, Node3D, NodeExt, PackedScene, SceneTree}; -use godot::obj::Share; use crate::framework::{itest, TestContext}; @@ -20,11 +19,11 @@ fn node_get_node() { let mut parent = Node3D::new_alloc(); parent.set_name("parent".into()); - parent.add_child(child.share().upcast()); + parent.add_child(child.clone().upcast()); let mut grandparent = Node::new_alloc(); grandparent.set_name("grandparent".into()); - grandparent.add_child(parent.share().upcast()); + grandparent.add_child(parent.clone().upcast()); // Directly on Gd let found = grandparent.get_node_as::(NodePath::from("parent/child")); @@ -51,7 +50,7 @@ fn node_get_node_fail() { #[itest] fn node_path_from_str(ctx: &TestContext) { - let child = ctx.scene_tree.share(); + let child = ctx.scene_tree.clone(); assert_eq!( child.get_path().to_string(), NodePath::from_str("/root/TestRunner").unwrap().to_string() @@ -65,10 +64,10 @@ fn node_scene_tree() { let mut parent = Node::new_alloc(); parent.set_name("parent".into()); - parent.add_child(child.share()); + parent.add_child(child.clone()); let mut scene = PackedScene::new(); - let err = scene.pack(parent.share()); + let err = scene.pack(parent.clone()); assert_eq!(err, global::Error::OK); let mut tree = SceneTree::new_alloc(); @@ -84,7 +83,7 @@ fn node_scene_tree() { #[itest] fn node_call_group(ctx: &TestContext) { - let mut node = ctx.scene_tree.share(); + let mut node = ctx.scene_tree.clone(); let mut tree = node.get_tree().unwrap(); node.add_to_group("group".into()); diff --git a/itest/rust/src/object_tests/object_test.rs b/itest/rust/src/object_tests/object_test.rs index 9a4c81f1d..e90a8b47e 100644 --- a/itest/rust/src/object_tests/object_test.rs +++ b/itest/rust/src/object_tests/object_test.rs @@ -14,8 +14,7 @@ use godot::builtin::{ use godot::engine::{ file_access, Area2D, Camera3D, FileAccess, Node, Node3D, Object, RefCounted, RefCountedVirtual, }; -use godot::obj::{Base, Gd, InstanceId}; -use godot::obj::{Inherits, Share}; +use godot::obj::{Base, Gd, Inherits, InstanceId}; use godot::sys::{self, GodotFfi}; use crate::framework::{expect_panic, itest, TestContext}; @@ -166,7 +165,7 @@ fn object_instance_id_when_freed() { let node: Gd = Node3D::new_alloc(); assert!(node.is_instance_valid()); - node.share().free(); // destroys object without moving out of reference + node.clone().free(); // destroys object without moving out of reference assert!(!node.is_instance_valid()); expect_panic("instance_id() on dead object", move || { @@ -219,7 +218,7 @@ fn object_user_eq() { let b = ObjPayload { value }; let a1 = Gd::new(a); - let a2 = a1.share(); + let a2 = a1.clone(); let b1 = Gd::new(b); assert_eq!(a1, a2); @@ -230,7 +229,7 @@ fn object_user_eq() { #[itest] fn object_engine_eq() { let a1 = Node3D::new_alloc(); - let a2 = a1.share(); + let a2 = a1.clone(); let b1 = Node3D::new_alloc(); assert_eq!(a1, a2); @@ -245,19 +244,19 @@ fn object_engine_eq() { fn object_dead_eq() { let a = Node3D::new_alloc(); let b = Node3D::new_alloc(); - let b2 = b.share(); + let b2 = b.clone(); // Destroy b1 without consuming it - b.share().free(); + b.clone().free(); { - let lhs = a.share(); + let lhs = a.clone(); expect_panic("Gd::eq() panics when one operand is dead", move || { let _ = lhs == b; }); } { - let rhs = a.share(); + let rhs = a.clone(); expect_panic("Gd::ne() panics when one operand is dead", move || { let _ = b2 != rhs; }); @@ -465,7 +464,7 @@ fn object_engine_downcast_reflexive() { #[itest] fn object_engine_bad_downcast() { let object: Gd = Object::new_alloc(); - let free_ref = object.share(); + let free_ref = object.clone(); let node3d: Option> = object.try_cast::(); assert!(node3d.is_none()); @@ -480,10 +479,10 @@ fn object_engine_accept_polymorphic() { node.set_name(GodotString::from(&expected_name)); - let actual_name = accept_node(node.share()); + let actual_name = accept_node(node.clone()); assert_eq!(actual_name, expected_name); - let actual_class = accept_object(node.share()); + let actual_class = accept_object(node.clone()); assert_eq!(actual_class, expected_class); node.free(); @@ -494,7 +493,7 @@ fn object_user_accept_polymorphic() { let obj = Gd::new(ObjPayload { value: 123 }); let expected_class = GodotString::from("ObjPayload"); - let actual_class = accept_refcounted(obj.share()); + let actual_class = accept_refcounted(obj.clone()); assert_eq!(actual_class, expected_class); let actual_class = accept_object(obj); @@ -564,7 +563,7 @@ fn object_engine_manual_free() { { let node = Node3D::new_alloc(); - let node2 = node.share(); + let node2 = node.clone(); node2.free(); } // drop(node) } @@ -574,7 +573,7 @@ fn object_engine_manual_free() { fn object_engine_shared_free() { { let node = Node::new_alloc(); - let _object = node.share().upcast::(); + let _object = node.clone().upcast::(); node.free(); } // drop(_object) } @@ -583,7 +582,7 @@ fn object_engine_shared_free() { fn object_engine_manual_double_free() { expect_panic("double free()", || { let node = Node3D::new_alloc(); - let node2 = node.share(); + let node2 = node.clone(); node.free(); node2.free(); }); @@ -592,7 +591,7 @@ fn object_engine_manual_double_free() { #[itest] fn object_engine_refcounted_free() { let node = RefCounted::new(); - let node2 = node.share().upcast::(); + let node2 = node.clone().upcast::(); expect_panic("calling free() on RefCounted object", || node2.free()) } @@ -606,7 +605,7 @@ fn object_user_share_drop() { }); assert_eq!(*drop_count.borrow(), 0); - let shared = object.share(); + let shared = object.clone(); assert_eq!(*drop_count.borrow(), 0); drop(shared); @@ -650,7 +649,7 @@ fn object_call_with_args() { fn object_get_scene_tree(ctx: &TestContext) { let node = Node3D::new_alloc(); - let mut tree = ctx.scene_tree.share(); + let mut tree = ctx.scene_tree.clone(); tree.add_child(node.upcast()); let count = tree.get_child_count(); @@ -820,7 +819,7 @@ fn double_use_reference() { let emitter: Gd = Gd::new_default(); emitter - .share() + .clone() .upcast::() .connect("do_use".into(), double_use.callable("use_1")); @@ -829,7 +828,7 @@ fn double_use_reference() { assert!(!guard.used.get()); emitter - .share() + .clone() .upcast::() .emit_signal("do_use".into(), &[]); diff --git a/itest/rust/src/object_tests/virtual_methods_test.rs b/itest/rust/src/object_tests/virtual_methods_test.rs index c2dd9d040..5cfda5393 100644 --- a/itest/rust/src/object_tests/virtual_methods_test.rs +++ b/itest/rust/src/object_tests/virtual_methods_test.rs @@ -21,7 +21,7 @@ use godot::engine::{ PrimitiveMeshVirtual, RefCounted, RefCountedVirtual, ResourceFormatLoader, ResourceFormatLoaderVirtual, ResourceLoader, RigidBody2DVirtual, Viewport, Window, }; -use godot::obj::{Base, Gd, Share}; +use godot::obj::{Base, Gd}; use godot::private::class_macros::assert_eq_approx; /// Simple class, that deliberately has no constructor accessible from GDScript @@ -239,8 +239,8 @@ fn test_ready(test_context: &TestContext) { assert_eq!(obj.bind().implementation_value, 0); // Add to scene tree - let mut test_node = test_context.scene_tree.share(); - test_node.add_child(obj.share().upcast()); + let mut test_node = test_context.scene_tree.clone(); + test_node.add_child(obj.clone().upcast()); // _ready runs, increments implementation_value once. assert_eq!(obj.bind().implementation_value, 1); @@ -251,17 +251,17 @@ fn test_ready_multiple_fires(test_context: &TestContext) { let obj = Gd::::new_default(); assert_eq!(obj.bind().implementation_value, 0); - let mut test_node = test_context.scene_tree.share(); + let mut test_node = test_context.scene_tree.clone(); // Add to scene tree - test_node.add_child(obj.share().upcast()); + test_node.add_child(obj.clone().upcast()); // _ready runs, increments implementation_value once. assert_eq!(obj.bind().implementation_value, 1); // Remove and re-add to scene tree - test_node.remove_child(obj.share().upcast()); - test_node.add_child(obj.share().upcast()); + test_node.remove_child(obj.clone().upcast()); + test_node.add_child(obj.clone().upcast()); // _ready does NOT run again, implementation_value should still be 1. assert_eq!(obj.bind().implementation_value, 1); @@ -272,27 +272,27 @@ fn test_ready_request_ready(test_context: &TestContext) { let obj = Gd::::new_default(); assert_eq!(obj.bind().implementation_value, 0); - let mut test_node = test_context.scene_tree.share(); + let mut test_node = test_context.scene_tree.clone(); // Add to scene tree - test_node.add_child(obj.share().upcast()); + test_node.add_child(obj.clone().upcast()); // _ready runs, increments implementation_value once. assert_eq!(obj.bind().implementation_value, 1); // Remove and re-add to scene tree - test_node.remove_child(obj.share().upcast()); - test_node.add_child(obj.share().upcast()); + test_node.remove_child(obj.clone().upcast()); + test_node.add_child(obj.clone().upcast()); // _ready does NOT run again, implementation_value should still be 1. assert_eq!(obj.bind().implementation_value, 1); // Request ready - obj.share().upcast::().request_ready(); + obj.clone().upcast::().request_ready(); // Remove and re-add to scene tree - test_node.remove_child(obj.share().upcast()); - test_node.add_child(obj.share().upcast()); + test_node.remove_child(obj.clone().upcast()); + test_node.add_child(obj.clone().upcast()); // _ready runs again since we asked it to; implementation_value should be 2. assert_eq!(obj.bind().implementation_value, 2); @@ -303,18 +303,18 @@ fn test_tree_enters_exits(test_context: &TestContext) { let obj = Gd::::new_default(); assert_eq!(obj.bind().tree_enters, 0); assert_eq!(obj.bind().tree_exits, 0); - let mut test_node = test_context.scene_tree.share(); + let mut test_node = test_context.scene_tree.clone(); // Add to scene tree - test_node.add_child(obj.share().upcast()); + test_node.add_child(obj.clone().upcast()); assert_eq!(obj.bind().tree_enters, 1); assert_eq!(obj.bind().tree_exits, 0); // Remove and re-add to scene tree - test_node.remove_child(obj.share().upcast()); + test_node.remove_child(obj.clone().upcast()); assert_eq!(obj.bind().tree_enters, 1); assert_eq!(obj.bind().tree_exits, 1); - test_node.add_child(obj.share().upcast()); + test_node.add_child(obj.clone().upcast()); assert_eq!(obj.bind().tree_enters, 2); assert_eq!(obj.bind().tree_exits, 1); } @@ -322,7 +322,7 @@ fn test_tree_enters_exits(test_context: &TestContext) { #[itest] fn test_virtual_method_with_return() { let obj = Gd::::new_default(); - let arr = obj.share().upcast::().get_mesh_arrays(); + let arr = obj.clone().upcast::().get_mesh_arrays(); let arr_rust = obj.bind().create_mesh_array(); assert_eq!(arr.len(), arr_rust.len()); // can't just assert_eq because the values of some floats change slightly @@ -357,7 +357,7 @@ fn test_format_loader(_test_context: &TestContext) { let format_loader = Gd::::new_default(); let mut loader = ResourceLoader::singleton(); loader - .add_resource_format_loader_ex(format_loader.share().upcast()) + .add_resource_format_loader_ex(format_loader.clone().upcast()) .at_front(true) .done(); @@ -383,17 +383,17 @@ fn test_input_event(test_context: &TestContext) { test_context .scene_tree - .share() - .add_child(test_viewport.share().upcast()); + .clone() + .add_child(test_viewport.clone().upcast()); - test_viewport.share().add_child(obj.share().upcast()); + test_viewport.clone().add_child(obj.clone().upcast()); let mut event = InputEventAction::new(); event.set_action("debug".into()); event.set_pressed(true); // We're running in headless mode, so Input.parse_input_event does not work - test_viewport.share().push_input(event.share().upcast()); + test_viewport.clone().push_input(event.clone().upcast()); assert_eq!(obj.bind().event, Some(event.upcast::())); @@ -414,11 +414,11 @@ fn test_input_event_multiple(test_context: &TestContext) { test_context .scene_tree - .share() - .add_child(test_viewport.share().upcast()); + .clone() + .add_child(test_viewport.clone().upcast()); for obj in objs.iter() { - test_viewport.share().add_child(obj.share().upcast()) + test_viewport.clone().add_child(obj.clone().upcast()) } let mut event = InputEventAction::new(); @@ -426,10 +426,10 @@ fn test_input_event_multiple(test_context: &TestContext) { event.set_pressed(true); // We're running in headless mode, so Input.parse_input_event does not work - test_viewport.share().push_input(event.share().upcast()); + test_viewport.clone().push_input(event.clone().upcast()); for obj in objs.iter() { - assert_eq!(obj.bind().event, Some(event.share().upcast::())); + assert_eq!(obj.bind().event, Some(event.clone().upcast::())); } test_viewport.queue_free(); @@ -438,7 +438,7 @@ fn test_input_event_multiple(test_context: &TestContext) { #[itest] fn test_notifications() { let obj = Gd::::new_default(); - let mut node = obj.share().upcast::(); + let mut node = obj.clone().upcast::(); node.notify(NodeNotification::Unpaused); node.notify(NodeNotification::EditorPostSave); node.notify(NodeNotification::Ready);