Skip to content

Commit b5fa2e3

Browse files
committed
debug-display-for-builtins
1 parent 0b93fe1 commit b5fa2e3

File tree

7 files changed

+80
-1
lines changed

7 files changed

+80
-1
lines changed

godot-core/src/builtin/array.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,27 @@ impl<T: GodotType> fmt::Debug for Array<T> {
675675
}
676676
}
677677

678+
impl<T: GodotType + fmt::Display> fmt::Display for Array<T> {
679+
/// Formats `Array` to match Godot's string representation.
680+
///
681+
/// Example:
682+
/// ```no_run
683+
/// # use godot::prelude::*;
684+
/// let a = array![1,2,3,4];
685+
/// assert_eq!(format!("{a}"), "[1, 2, 3, 4]");
686+
/// ```
687+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
688+
write!(f, "[")?;
689+
for (count, v) in self.iter_shared().enumerate() {
690+
if count != 0 {
691+
write!(f, ", ")?;
692+
}
693+
write!(f, "{v}")?;
694+
}
695+
write!(f, "]")
696+
}
697+
}
698+
678699
/// Creates a new reference to the data in this array. Changes to the original array will be
679700
/// reflected in the copy and vice versa.
680701
///

godot-core/src/builtin/dictionary.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,19 @@ impl fmt::Debug for Dictionary {
308308
write!(f, "{:?}", self.to_variant().stringify())
309309
}
310310
}
311+
impl fmt::Display for Dictionary {
312+
/// Formats `Dictionary` to match Godot's string representation.
313+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
314+
write!(f, "{{ ")?;
315+
for (count, (key, value)) in self.iter_shared().enumerate() {
316+
if count != 0 {
317+
write!(f, ", ")?;
318+
}
319+
write!(f, "{key}: {value}")?;
320+
}
321+
write!(f, " }}")
322+
}
323+
}
311324

312325
/// Creates a new reference to the data in this dictionary. Changes to the original dictionary will be
313326
/// reflected in the copy and vice versa.

godot-core/src/builtin/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ pub(crate) fn u8_to_bool(u: u8) -> bool {
135135
///
136136
/// _Godot equivalent: `@GlobalScope.Side`_
137137
#[doc(alias = "Side")]
138-
#[derive(Copy, Clone)]
138+
#[derive(Copy, Clone, Debug)]
139139
#[repr(C)]
140140
pub enum RectSide {
141141
Left = 0,

godot-core/src/builtin/packed_array.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,20 @@ macro_rules! impl_packed_array {
444444
}
445445
}
446446

447+
impl fmt::Display for $PackedArray {
448+
/// Formats `PackedArray` to match Godot's string representation.
449+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
450+
write!(f, "[")?;
451+
for i in 0..self.len() {
452+
if i != 0 {
453+
write!(f, ", ")?;
454+
}
455+
write!(f, "{}", self.get(i))?;
456+
}
457+
write!(f, "]")
458+
}
459+
}
460+
447461
unsafe impl GodotFfi for $PackedArray {
448462
fn variant_type() -> sys::VariantType {
449463
sys::VariantType::$PackedArray

itest/rust/src/builtin_tests/containers/array_test.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,15 @@ fn untyped_array_try_from_typed() {
452452
node.free();
453453
}
454454

455+
#[itest]
456+
fn array_should_format_with_display() {
457+
let a = array![1, 2, 3, 4];
458+
assert_eq!(format!("{a}"), "[1, 2, 3, 4]");
459+
460+
let a = Array::<real>::new();
461+
assert_eq!(format!("{a}"), "[]");
462+
}
463+
455464
#[derive(GodotClass, Debug)]
456465
#[class(init, base=RefCounted)]
457466
struct ArrayTest;

itest/rust/src/builtin_tests/containers/dictionary_test.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,3 +615,16 @@ fn dictionary_iter_erase() {
615615
```
616616
*/
617617
}
618+
619+
#[itest]
620+
fn dictionary_should_format_with_display() {
621+
let d = Dictionary::new();
622+
assert_eq!(format!("{d}"), "{ }");
623+
624+
let d = dict! {
625+
"one": 1,
626+
"two": true,
627+
"three": Variant::nil()
628+
};
629+
assert_eq!(format!("{d}"), "{ one: 1, two: true, three: <null> }")
630+
}

itest/rust/src/builtin_tests/containers/packed_array_test.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,12 @@ fn packed_array_reverse() {
224224
array.reverse();
225225
assert_eq!(array.to_vec(), vec![2, 1]);
226226
}
227+
228+
#[itest]
229+
fn packed_array_format() {
230+
let a = PackedByteArray::from(&[2, 1]);
231+
assert_eq!(format!("{a}"), "[2, 1]");
232+
233+
let a = PackedByteArray::new();
234+
assert_eq!(format!("{a}"), "[]");
235+
}

0 commit comments

Comments
 (0)