diff --git a/godot-core/src/builtin/array.rs b/godot-core/src/builtin/array.rs index 2bfaccce1..5134c7ba9 100644 --- a/godot-core/src/builtin/array.rs +++ b/godot-core/src/builtin/array.rs @@ -675,6 +675,27 @@ impl fmt::Debug for Array { } } +impl fmt::Display for Array { + /// Formats `Array` to match Godot's string representation. + /// + /// Example: + /// ```no_run + /// # use godot::prelude::*; + /// let a = array![1,2,3,4]; + /// assert_eq!(format!("{a}"), "[1, 2, 3, 4]"); + /// ``` + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "[")?; + for (count, v) in self.iter_shared().enumerate() { + if count != 0 { + write!(f, ", ")?; + } + write!(f, "{v}")?; + } + write!(f, "]") + } +} + /// Creates a new reference to the data in this array. Changes to the original array will be /// reflected in the copy and vice versa. /// diff --git a/godot-core/src/builtin/dictionary.rs b/godot-core/src/builtin/dictionary.rs index 6941ebacf..dd5c2a90f 100644 --- a/godot-core/src/builtin/dictionary.rs +++ b/godot-core/src/builtin/dictionary.rs @@ -308,6 +308,19 @@ impl fmt::Debug for Dictionary { write!(f, "{:?}", self.to_variant().stringify()) } } +impl fmt::Display for Dictionary { + /// Formats `Dictionary` to match Godot's string representation. + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{{ ")?; + for (count, (key, value)) in self.iter_shared().enumerate() { + if count != 0 { + write!(f, ", ")?; + } + write!(f, "{key}: {value}")?; + } + write!(f, " }}") + } +} /// Creates a new reference to the data in this dictionary. Changes to the original dictionary will be /// reflected in the copy and vice versa. diff --git a/godot-core/src/builtin/mod.rs b/godot-core/src/builtin/mod.rs index 33a167935..a633d3b22 100644 --- a/godot-core/src/builtin/mod.rs +++ b/godot-core/src/builtin/mod.rs @@ -135,7 +135,7 @@ pub(crate) fn u8_to_bool(u: u8) -> bool { /// /// _Godot equivalent: `@GlobalScope.Side`_ #[doc(alias = "Side")] -#[derive(Copy, Clone)] +#[derive(Copy, Clone, Debug)] #[repr(C)] pub enum RectSide { Left = 0, diff --git a/godot-core/src/builtin/packed_array.rs b/godot-core/src/builtin/packed_array.rs index 89b7e142a..8d464f7aa 100644 --- a/godot-core/src/builtin/packed_array.rs +++ b/godot-core/src/builtin/packed_array.rs @@ -444,6 +444,20 @@ macro_rules! impl_packed_array { } } + impl fmt::Display for $PackedArray { + /// Formats `PackedArray` to match Godot's string representation. + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "[")?; + for i in 0..self.len() { + if i != 0 { + write!(f, ", ")?; + } + write!(f, "{}", self.get(i))?; + } + write!(f, "]") + } + } + unsafe impl GodotFfi for $PackedArray { fn variant_type() -> sys::VariantType { sys::VariantType::$PackedArray diff --git a/itest/rust/src/builtin_tests/containers/array_test.rs b/itest/rust/src/builtin_tests/containers/array_test.rs index 7beb3ae15..2f8d02f60 100644 --- a/itest/rust/src/builtin_tests/containers/array_test.rs +++ b/itest/rust/src/builtin_tests/containers/array_test.rs @@ -452,6 +452,15 @@ fn untyped_array_try_from_typed() { node.free(); } +#[itest] +fn array_should_format_with_display() { + let a = array![1, 2, 3, 4]; + assert_eq!(format!("{a}"), "[1, 2, 3, 4]"); + + let a = Array::::new(); + assert_eq!(format!("{a}"), "[]"); +} + #[derive(GodotClass, Debug)] #[class(init, base=RefCounted)] struct ArrayTest; diff --git a/itest/rust/src/builtin_tests/containers/dictionary_test.rs b/itest/rust/src/builtin_tests/containers/dictionary_test.rs index 979593cb1..ec768997e 100644 --- a/itest/rust/src/builtin_tests/containers/dictionary_test.rs +++ b/itest/rust/src/builtin_tests/containers/dictionary_test.rs @@ -615,3 +615,16 @@ fn dictionary_iter_erase() { ``` */ } + +#[itest] +fn dictionary_should_format_with_display() { + let d = Dictionary::new(); + assert_eq!(format!("{d}"), "{ }"); + + let d = dict! { + "one": 1, + "two": true, + "three": Variant::nil() + }; + assert_eq!(format!("{d}"), "{ one: 1, two: true, three: }") +} diff --git a/itest/rust/src/builtin_tests/containers/packed_array_test.rs b/itest/rust/src/builtin_tests/containers/packed_array_test.rs index 815cdb7a8..03eac07c6 100644 --- a/itest/rust/src/builtin_tests/containers/packed_array_test.rs +++ b/itest/rust/src/builtin_tests/containers/packed_array_test.rs @@ -224,3 +224,12 @@ fn packed_array_reverse() { array.reverse(); assert_eq!(array.to_vec(), vec![2, 1]); } + +#[itest] +fn packed_array_format() { + let a = PackedByteArray::from(&[2, 1]); + assert_eq!(format!("{a}"), "[2, 1]"); + + let a = PackedByteArray::new(); + assert_eq!(format!("{a}"), "[]"); +}