File tree Expand file tree Collapse file tree 4 files changed +26
-9
lines changed
itest/rust/src/builtin_tests/containers Expand file tree Collapse file tree 4 files changed +26
-9
lines changed Original file line number Diff line number Diff line change @@ -205,8 +205,8 @@ impl Callable {
205
205
206
206
/// Returns the object on which this callable is called.
207
207
///
208
- /// Returns `None` when this callable doesn't have any target object to call a method on, regardless of
209
- /// if the method exists for that target or not .
208
+ /// Returns `None` when this callable doesn't have any target object to call a method on ( regardless of whether the method exists for that
209
+ /// target or not). Also returns `None` if the object is dead. You can differentiate these two cases using [`object_id()`][Self::object_id] .
210
210
///
211
211
/// _Godot equivalent: `get_object`_
212
212
pub fn object ( & self ) -> Option < Gd < Object > > {
@@ -222,6 +222,8 @@ impl Callable {
222
222
///
223
223
/// Returns `None` when this callable doesn't have any target to call a method on.
224
224
///
225
+ /// If the pointed-to object is dead, the ID will still be returned. Use [`object()`][Self::object] to check for liveness.
226
+ ///
225
227
/// _Godot equivalent: `get_object_id`_
226
228
pub fn object_id ( & self ) -> Option < InstanceId > {
227
229
let id = self . as_inner ( ) . get_object_id ( ) ;
Original file line number Diff line number Diff line change @@ -121,7 +121,8 @@ impl Signal {
121
121
122
122
/// Returns the object to which this signal belongs.
123
123
///
124
- /// Returns [`None`] when this signal doesn't have any object.
124
+ /// Returns [`None`] when this signal doesn't have any object, or the object is dead. You can differentiate these two situations using
125
+ /// [`object_id()`][Self::object_id].
125
126
///
126
127
/// _Godot equivalent: `get_object`_
127
128
pub fn object ( & self ) -> Option < Gd < Object > > {
@@ -135,6 +136,8 @@ impl Signal {
135
136
///
136
137
/// Returns [`None`] when this signal doesn't have any object.
137
138
///
139
+ /// If the pointed-to object is dead, the ID will still be returned. Use [`object()`][Self::object] to check for liveness.
140
+ ///
138
141
/// _Godot equivalent: `get_object_id`_
139
142
pub fn object_id ( & self ) -> Option < InstanceId > {
140
143
let id = self . as_inner ( ) . get_object_id ( ) ;
Original file line number Diff line number Diff line change @@ -67,13 +67,19 @@ fn callable_hash() {
67
67
68
68
#[ itest]
69
69
fn callable_object_method ( ) {
70
- let obj = CallableTestObj :: new_gd ( ) ;
71
- let callable = obj. callable ( "foo" ) ;
70
+ let object = CallableTestObj :: new_gd ( ) ;
71
+ let object_id = object. instance_id ( ) ;
72
+ let callable = object. callable ( "foo" ) ;
72
73
73
- assert_eq ! ( callable. object( ) , Some ( obj . clone( ) . upcast:: <Object >( ) ) ) ;
74
- assert_eq ! ( callable. object_id( ) , Some ( obj . instance_id ( ) ) ) ;
74
+ assert_eq ! ( callable. object( ) , Some ( object . clone( ) . upcast:: <Object >( ) ) ) ;
75
+ assert_eq ! ( callable. object_id( ) , Some ( object_id ) ) ;
75
76
assert_eq ! ( callable. method_name( ) , Some ( "foo" . into( ) ) ) ;
76
77
78
+ // Invalidating the object still returns the old ID, however not the object.
79
+ drop ( object) ;
80
+ assert_eq ! ( callable. object_id( ) , Some ( object_id) ) ;
81
+ assert_eq ! ( callable. object( ) , None ) ;
82
+
77
83
assert_eq ! ( Callable :: invalid( ) . object( ) , None ) ;
78
84
assert_eq ! ( Callable :: invalid( ) . object_id( ) , None ) ;
79
85
assert_eq ! ( Callable :: invalid( ) . method_name( ) , None ) ;
Original file line number Diff line number Diff line change @@ -95,15 +95,21 @@ fn signals() {
95
95
#[ itest]
96
96
fn instantiate_signal ( ) {
97
97
let mut object = RefCounted :: new_gd ( ) ;
98
+ let object_id = object. instance_id ( ) ;
98
99
99
100
object. add_user_signal ( "test_signal" . into ( ) ) ;
100
101
101
102
let signal = Signal :: from_object_signal ( & object, "test_signal" ) ;
102
103
103
104
assert ! ( !signal. is_null( ) ) ;
104
105
assert_eq ! ( signal. name( ) , StringName :: from( "test_signal" ) ) ;
105
- assert_eq ! ( signal. object( ) . unwrap( ) , object. clone( ) . upcast( ) ) ;
106
- assert_eq ! ( signal. object_id( ) . unwrap( ) , object. instance_id( ) ) ;
106
+ assert_eq ! ( signal. object( ) , Some ( object. clone( ) . upcast( ) ) ) ;
107
+ assert_eq ! ( signal. object_id( ) , Some ( object_id) ) ;
108
+
109
+ // Invalidating the object still returns the old ID, however not the object.
110
+ drop ( object) ;
111
+ assert_eq ! ( signal. object_id( ) , Some ( object_id) ) ;
112
+ assert_eq ! ( signal. object( ) , None ) ;
107
113
}
108
114
109
115
#[ itest]
You can’t perform that action at this time.
0 commit comments