Skip to content

Commit d63c0a9

Browse files
bors[bot]hapenia
andauthored
Merge #224
224: Impl `Display` for built-ins r=Bromeon a=Hapenia-Lans related to #110 Co-authored-by: ChenXi <[email protected]>
2 parents 21401a1 + fc8e6bb commit d63c0a9

File tree

8 files changed

+139
-2
lines changed

8 files changed

+139
-2
lines changed

godot-core/src/builtin/aabb.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,20 @@ impl Aabb {
8282
*/
8383
}
8484

85+
impl std::fmt::Display for Aabb {
86+
/// Formats `Aabb` to match godot's display style.
87+
///
88+
/// Example:
89+
/// ```
90+
/// use godot::prelude::*;
91+
/// let aabb = Aabb::new(Vector3::new(0.0, 0.0, 0.0), Vector3::new(1.0, 1.0, 1.0));
92+
/// assert_eq!(format!("{}", aabb), "[P: (0, 0, 0), S: (1, 1, 1)]");
93+
/// ```
94+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
95+
write!(f, "[P: {}, S: {}]", self.position, self.size)
96+
}
97+
}
98+
8599
// SAFETY:
86100
// This type is represented as `Self` in Godot, so `*mut Self` is sound.
87101
unsafe impl GodotFfi for Aabb {

godot-core/src/builtin/color.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,3 +497,17 @@ fn to_be_words(mut u: u64) -> [u16; 4] {
497497
let x = (u & 0xffff) as u16;
498498
[x, y, z, w]
499499
}
500+
501+
impl std::fmt::Display for Color {
502+
/// Formats `Color` to match Godot's string representation.
503+
///
504+
/// Example:
505+
/// ```
506+
/// use godot::prelude::*;
507+
/// let color = Color::from_rgba(1.0,1.0,1.0,1.0);
508+
/// assert_eq!(format!("{}", color), "(1, 1, 1, 1)");
509+
/// ```
510+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
511+
write!(f, "({}, {}, {}, {})", self.r, self.g, self.b, self.a)
512+
}
513+
}

godot-core/src/builtin/plane.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,20 @@ unsafe impl GodotFfi for Plane {
140140
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
141141
}
142142

143+
impl std::fmt::Display for Plane {
144+
/// Formats `Plane` to match Godot's string representation.
145+
///
146+
/// Example:
147+
/// ```
148+
/// use godot::prelude::*;
149+
/// let plane = Plane::new(Vector3::new(1.0, 0.0, 0.0), 1.0);
150+
/// assert_eq!(format!("{}", plane), "[N: (1, 0, 0), D: 1]");
151+
/// ```
152+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
153+
write!(f, "[N: {}, D: {}]", self.normal, self.d)
154+
}
155+
}
156+
143157
#[cfg(test)]
144158
mod test {
145159
use super::*;

godot-core/src/builtin/projection.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use godot_ffi as sys;
99
use sys::{ffi_methods, GodotFfi};
1010

1111
use super::glam_helpers::{GlamConv, GlamType};
12-
use super::{inner::InnerProjection, Plane, Transform3D, Vector2, Vector4};
12+
use super::{inner::InnerProjection, Plane, Transform3D, Vector2, Vector4, Vector4Axis};
1313
use super::{real, RMat4, RealConv};
1414

1515
/// A 4x4 matrix used for 3D projective transformations. It can represent
@@ -961,3 +961,51 @@ mod test {
961961
}
962962
}
963963
}
964+
965+
impl std::fmt::Display for Projection {
966+
/// Formats `Projection` to match Godot's string representation.
967+
///
968+
/// Example:
969+
/// ```
970+
/// use godot::prelude::*;
971+
/// let proj = Projection::new([
972+
/// Vector4::new(1.0, 2.5, 1.0, 0.5),
973+
/// Vector4::new(0.0, 1.5, 2.0, 0.5),
974+
/// Vector4::new(0.0, 0.0, 3.0, 2.5),
975+
/// Vector4::new(3.0, 1.0, 4.0, 1.5),
976+
/// ]);
977+
/// const FMT_RESULT: &str = r"
978+
/// 1, 0, 0, 3
979+
/// 2.5, 1.5, 0, 1
980+
/// 1, 2, 3, 4
981+
/// 0.5, 0.5, 2.5, 1.5
982+
/// ";
983+
/// assert_eq!(format!("{}", proj), FMT_RESULT);
984+
/// ```
985+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
986+
write!(
987+
f,
988+
"\n{}, {}, {}, {}\n{}, {}, {}, {}\n{}, {}, {}, {}\n{}, {}, {}, {}\n",
989+
// first row
990+
self.cols[0][Vector4Axis::X],
991+
self.cols[1][Vector4Axis::X],
992+
self.cols[2][Vector4Axis::X],
993+
self.cols[3][Vector4Axis::X],
994+
// second row
995+
self.cols[0][Vector4Axis::Y],
996+
self.cols[1][Vector4Axis::Y],
997+
self.cols[2][Vector4Axis::Y],
998+
self.cols[3][Vector4Axis::Y],
999+
// third row
1000+
self.cols[0][Vector4Axis::Z],
1001+
self.cols[1][Vector4Axis::Z],
1002+
self.cols[2][Vector4Axis::Z],
1003+
self.cols[3][Vector4Axis::Z],
1004+
// forth row
1005+
self.cols[0][Vector4Axis::W],
1006+
self.cols[1][Vector4Axis::W],
1007+
self.cols[2][Vector4Axis::W],
1008+
self.cols[3][Vector4Axis::W],
1009+
)
1010+
}
1011+
}

godot-core/src/builtin/rect2.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,19 @@ impl Rect2 {
109109
unsafe impl GodotFfi for Rect2 {
110110
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
111111
}
112+
113+
impl std::fmt::Display for Rect2 {
114+
/// Formats `Rect2` to match Godot's string representation.
115+
///
116+
/// Example:
117+
/// ```
118+
/// use godot::prelude::*;
119+
/// let rect = Rect2::new(Vector2::new(0.0, 0.0), Vector2::new(1.0, 1.0));
120+
/// assert_eq!(format!("{}", rect), "[P: (0, 0), S: (1, 1)]");
121+
/// ```
122+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
123+
// godot output be like:
124+
// [P: (0, 0), S: (0, 0)]
125+
write!(f, "[P: {}, S: {}]", self.position, self.size)
126+
}
127+
}

godot-core/src/builtin/rect2i.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,20 @@ unsafe impl GodotFfi for Rect2i {
267267
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
268268
}
269269

270+
impl std::fmt::Display for Rect2i {
271+
/// Formats `Rect2i` to match Godot's string representation.
272+
///
273+
/// Example:
274+
/// ```
275+
/// use godot::prelude::*;
276+
/// let rect = Rect2i::new(Vector2i::new(0, 0), Vector2i::new(1, 1));
277+
/// assert_eq!(format!("{}", rect), "[P: (0, 0), S: (1, 1)]");
278+
/// ```
279+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
280+
write!(f, "[P: {}, S: {}]", self.position, self.size)
281+
}
282+
}
283+
270284
#[cfg(test)]
271285
mod test {
272286
use super::*;

godot-core/src/builtin/rid.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,24 @@ impl Rid {
8181
}
8282
}
8383

84+
impl std::fmt::Display for Rid {
85+
/// Formats `Rid` to match Godot's string representation.
86+
///
87+
/// Example:
88+
/// ```
89+
/// use godot::prelude::*;
90+
/// let id = Rid::new(1);
91+
/// assert_eq!(format!("{}", id), "RID(1)");
92+
/// ```
93+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
94+
// godot output: `RID(0)`
95+
match self {
96+
Rid::Valid(x) => write!(f, "RID({})", x),
97+
Rid::Invalid => write!(f, "RID(0)"),
98+
}
99+
}
100+
}
101+
84102
// SAFETY:
85103
// This type is represented as `Self` in Godot, so `*mut Self` is sound.
86104
unsafe impl GodotFfi for Rid {

godot-core/src/builtin/transform2d.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,6 @@ impl Default for Basis2D {
496496
impl Display for Basis2D {
497497
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
498498
let [a, b] = self.cols;
499-
500499
write!(f, "[a: {a}, b: {b})]")
501500
}
502501
}

0 commit comments

Comments
 (0)