Skip to content

Impl Display for built-ins #224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions godot-core/src/builtin/aabb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ impl Aabb {
*/
}

impl std::fmt::Display for Aabb {
/// Formats `Aabb` to match godot's display style.
///
/// Example:
/// ```
Comment on lines +88 to +89
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You applied the changes only here, not in all the other cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I will fix it :)

/// use godot::prelude::*;
/// let aabb = Aabb::new(Vector3::new(0.0, 0.0, 0.0), Vector3::new(1.0, 1.0, 1.0));
/// assert_eq!(format!("{}", aabb), "[P: (0, 0, 0), S: (1, 1, 1)]");
/// ```
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "[P: {}, S: {}]", self.position, self.size)
}
}

// SAFETY:
// This type is represented as `Self` in Godot, so `*mut Self` is sound.
unsafe impl GodotFfi for Aabb {
Expand Down
14 changes: 14 additions & 0 deletions godot-core/src/builtin/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,3 +497,17 @@ fn to_be_words(mut u: u64) -> [u16; 4] {
let x = (u & 0xffff) as u16;
[x, y, z, w]
}

impl std::fmt::Display for Color {
/// Formats `Color` to match Godot's string representation.
///
/// Example:
/// ```
/// use godot::prelude::*;
/// let color = Color::from_rgba(1.0,1.0,1.0,1.0);
/// assert_eq!(format!("{}", color), "(1, 1, 1, 1)");
/// ```
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "({}, {}, {}, {})", self.r, self.g, self.b, self.a)
}
}
14 changes: 14 additions & 0 deletions godot-core/src/builtin/plane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,20 @@ unsafe impl GodotFfi for Plane {
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
}

impl std::fmt::Display for Plane {
/// Formats `Plane` to match Godot's string representation.
///
/// Example:
/// ```
/// use godot::prelude::*;
/// let plane = Plane::new(Vector3::new(1.0, 0.0, 0.0), 1.0);
/// assert_eq!(format!("{}", plane), "[N: (1, 0, 0), D: 1]");
/// ```
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "[N: {}, D: {}]", self.normal, self.d)
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
50 changes: 49 additions & 1 deletion godot-core/src/builtin/projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use godot_ffi as sys;
use sys::{ffi_methods, GodotFfi};

use super::glam_helpers::{GlamConv, GlamType};
use super::{inner::InnerProjection, Plane, Transform3D, Vector2, Vector4};
use super::{inner::InnerProjection, Plane, Transform3D, Vector2, Vector4, Vector4Axis};
use super::{real, RMat4, RealConv};

/// A 4x4 matrix used for 3D projective transformations. It can represent
Expand Down Expand Up @@ -961,3 +961,51 @@ mod test {
}
}
}

impl std::fmt::Display for Projection {
/// Formats `Projection` to match Godot's string representation.
///
/// Example:
/// ```
/// use godot::prelude::*;
/// let proj = Projection::new([
/// Vector4::new(1.0, 2.5, 1.0, 0.5),
/// Vector4::new(0.0, 1.5, 2.0, 0.5),
/// Vector4::new(0.0, 0.0, 3.0, 2.5),
/// Vector4::new(3.0, 1.0, 4.0, 1.5),
/// ]);
/// const FMT_RESULT: &str = r"
/// 1, 0, 0, 3
/// 2.5, 1.5, 0, 1
/// 1, 2, 3, 4
/// 0.5, 0.5, 2.5, 1.5
/// ";
/// assert_eq!(format!("{}", proj), FMT_RESULT);
/// ```
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"\n{}, {}, {}, {}\n{}, {}, {}, {}\n{}, {}, {}, {}\n{}, {}, {}, {}\n",
// first row
self.cols[0][Vector4Axis::X],
self.cols[1][Vector4Axis::X],
self.cols[2][Vector4Axis::X],
self.cols[3][Vector4Axis::X],
// second row
self.cols[0][Vector4Axis::Y],
self.cols[1][Vector4Axis::Y],
self.cols[2][Vector4Axis::Y],
self.cols[3][Vector4Axis::Y],
// third row
self.cols[0][Vector4Axis::Z],
self.cols[1][Vector4Axis::Z],
self.cols[2][Vector4Axis::Z],
self.cols[3][Vector4Axis::Z],
// forth row
self.cols[0][Vector4Axis::W],
self.cols[1][Vector4Axis::W],
self.cols[2][Vector4Axis::W],
self.cols[3][Vector4Axis::W],
)
}
}
16 changes: 16 additions & 0 deletions godot-core/src/builtin/rect2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,19 @@ impl Rect2 {
unsafe impl GodotFfi for Rect2 {
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
}

impl std::fmt::Display for Rect2 {
/// Formats `Rect2` to match Godot's string representation.
///
/// Example:
/// ```
/// use godot::prelude::*;
/// let rect = Rect2::new(Vector2::new(0.0, 0.0), Vector2::new(1.0, 1.0));
/// assert_eq!(format!("{}", rect), "[P: (0, 0), S: (1, 1)]");
/// ```
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// godot output be like:
// [P: (0, 0), S: (0, 0)]
write!(f, "[P: {}, S: {}]", self.position, self.size)
}
}
14 changes: 14 additions & 0 deletions godot-core/src/builtin/rect2i.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,20 @@ unsafe impl GodotFfi for Rect2i {
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
}

impl std::fmt::Display for Rect2i {
/// Formats `Rect2i` to match Godot's string representation.
///
/// Example:
/// ```
/// use godot::prelude::*;
/// let rect = Rect2i::new(Vector2i::new(0, 0), Vector2i::new(1, 1));
/// assert_eq!(format!("{}", rect), "[P: (0, 0), S: (1, 1)]");
/// ```
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "[P: {}, S: {}]", self.position, self.size)
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
18 changes: 18 additions & 0 deletions godot-core/src/builtin/rid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ impl Rid {
}
}

impl std::fmt::Display for Rid {
/// Formats `Rid` to match Godot's string representation.
///
/// Example:
/// ```
/// use godot::prelude::*;
/// let id = Rid::new(1);
/// assert_eq!(format!("{}", id), "RID(1)");
/// ```
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// godot output: `RID(0)`
match self {
Rid::Valid(x) => write!(f, "RID({})", x),
Rid::Invalid => write!(f, "RID(0)"),
}
}
}

// SAFETY:
// This type is represented as `Self` in Godot, so `*mut Self` is sound.
unsafe impl GodotFfi for Rid {
Expand Down
1 change: 0 additions & 1 deletion godot-core/src/builtin/transform2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,6 @@ impl Default for Basis2D {
impl Display for Basis2D {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let [a, b] = self.cols;

write!(f, "[a: {a}, b: {b})]")
}
}
Expand Down