Skip to content

Commit 1b413a5

Browse files
committed
Implement Display for user-provided Gd<T>
1 parent 2d59ee5 commit 1b413a5

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

godot-core/src/obj/gd.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -649,14 +649,7 @@ impl<T: GodotClass> PartialEq for Gd<T> {
649649

650650
impl<T: GodotClass> Eq for Gd<T> {}
651651

652-
impl<T> Display for Gd<T>
653-
where
654-
T: GodotClass<Declarer = dom::EngineDomain>,
655-
{
656-
// TODO support for user objects? should it return the engine repr, or a custom <T as Display>::fmt()?
657-
// If the latter, we would need to do something like impl<T> Display for Gd<T> where T: Display,
658-
// and thus implement it for each class separately (or blanket GodotClass/EngineClass/...).
659-
652+
impl<T: GodotClass> Display for Gd<T> {
660653
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
661654
engine::display_string(self, f)
662655
}

itest/rust/src/object_test.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,17 @@ fn object_engine_roundtrip() {
115115
}
116116

117117
#[itest]
118-
fn object_display() {
118+
fn object_user_display() {
119+
let obj = Gd::new(ObjPayload { value: 774 });
120+
121+
let actual = format!(".:{obj}:.");
122+
let expected = ".:value=774:.".to_string();
123+
124+
assert_eq!(actual, expected);
125+
}
126+
127+
#[itest]
128+
fn object_engine_display() {
119129
let obj = Node3D::new_alloc();
120130
let id = obj.instance_id();
121131

@@ -631,7 +641,6 @@ fn user_object() -> Gd<ObjPayload> {
631641
}
632642

633643
#[derive(GodotClass, Debug, Eq, PartialEq)]
634-
//#[class(init)]
635644
pub struct ObjPayload {
636645
value: i16,
637646
}
@@ -641,6 +650,10 @@ impl RefCountedVirtual for ObjPayload {
641650
fn init(_base: Base<Self::Base>) -> Self {
642651
Self { value: 111 }
643652
}
653+
654+
fn to_string(&self) -> GodotString {
655+
format!("value={}", self.value).into()
656+
}
644657
}
645658

646659
// ----------------------------------------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)