Skip to content

Commit 38c67e9

Browse files
committed
Implement Eq for Gd<T>
1 parent 1f628e1 commit 38c67e9

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

godot-core/src/obj/gd.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,19 @@ impl<T: GodotClass> ToVariant for Gd<T> {
626626
}
627627
}
628628

629+
impl<T: GodotClass> PartialEq for Gd<T> {
630+
/// ⚠️ Returns whether two `Gd` pointers point to the same object.
631+
///
632+
/// # Panics
633+
/// When `self` or `other` is dead.
634+
fn eq(&self, other: &Self) -> bool {
635+
// Panics when one is dead
636+
self.instance_id() == other.instance_id()
637+
}
638+
}
639+
640+
impl<T: GodotClass> Eq for Gd<T> {}
641+
629642
impl<T> Display for Gd<T>
630643
where
631644
T: GodotClass<Declarer = dom::EngineDomain>,

itest/rust/src/object_test.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,35 @@ fn object_from_instance_id_unrelated_type() {
161161
node.free();
162162
}
163163

164+
#[itest]
165+
fn object_user_eq() {
166+
let value: i16 = 17943;
167+
let a = ObjPayload { value };
168+
let b = ObjPayload { value };
169+
170+
let a1 = Gd::new(a);
171+
let a2 = a1.share();
172+
let b1 = Gd::new(b);
173+
174+
assert_eq!(a1, a2);
175+
assert_ne!(a1, b1);
176+
assert_ne!(a2, b1);
177+
}
178+
179+
#[itest]
180+
fn object_engine_eq() {
181+
let a1 = Node3D::new_alloc();
182+
let a2 = a1.share();
183+
let b1 = Node3D::new_alloc();
184+
185+
assert_eq!(a1, a2);
186+
assert_ne!(a1, b1);
187+
assert_ne!(a2, b1);
188+
189+
a1.free();
190+
b1.free();
191+
}
192+
164193
#[itest]
165194
fn object_user_convert_variant() {
166195
let value: i16 = 17943;

0 commit comments

Comments
 (0)