From 0541f13b268997c2ffb3783586dbd319a9973147 Mon Sep 17 00:00:00 2001 From: Benjamin Saunders Date: Sun, 12 Sep 2021 10:52:46 -0700 Subject: [PATCH] Concise Debug impls Replace the verbose derived (or nearly equivalent) Debug impls for several newtypes with explicit impls that forward to the inner type, making readable diagnostics logging much easier. --- src/base/matrix.rs | 5 +---- src/base/unit.rs | 9 ++++++++- src/geometry/point.rs | 11 ++++++++++- src/geometry/quaternion.rs | 8 +++++++- src/geometry/rotation.rs | 7 ++++++- src/geometry/transform.rs | 13 +++++++++++-- src/geometry/translation.rs | 7 ++++++- tests/core/matrix.rs | 4 ++-- 8 files changed, 51 insertions(+), 13 deletions(-) diff --git a/src/base/matrix.rs b/src/base/matrix.rs index 13413dbf..8d17a02d 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -195,10 +195,7 @@ pub struct Matrix { impl fmt::Debug for Matrix { fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { - formatter - .debug_struct("Matrix") - .field("data", &self.data) - .finish() + self.data.fmt(formatter) } } diff --git a/src/base/unit.rs b/src/base/unit.rs index cd32b44b..8d548687 100644 --- a/src/base/unit.rs +++ b/src/base/unit.rs @@ -1,3 +1,4 @@ +use std::fmt; #[cfg(feature = "abomonation-serialize")] use std::io::{Result as IOResult, Write}; use std::ops::Deref; @@ -24,11 +25,17 @@ use crate::{Dim, Matrix, OMatrix, RealField, Scalar, SimdComplexField, SimdRealF /// and [`UnitQuaternion`](crate::UnitQuaternion); both built on top of `Unit`. If you are interested /// in their documentation, read their dedicated pages directly. #[repr(transparent)] -#[derive(Clone, Hash, Debug, Copy)] +#[derive(Clone, Hash, Copy)] pub struct Unit { pub(crate) value: T, } +impl fmt::Debug for Unit { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { + self.value.fmt(formatter) + } +} + #[cfg(feature = "bytemuck")] unsafe impl bytemuck::Zeroable for Unit where T: bytemuck::Zeroable {} diff --git a/src/geometry/point.rs b/src/geometry/point.rs index 69022671..7930e8fb 100644 --- a/src/geometry/point.rs +++ b/src/geometry/point.rs @@ -40,7 +40,7 @@ use std::mem::MaybeUninit; /// may have some other methods, e.g., `isometry.inverse_transform_point(&point)`. See the documentation /// of said transformations for details. #[repr(C)] -#[derive(Debug, Clone)] +#[derive(Clone)] pub struct OPoint where DefaultAllocator: Allocator, @@ -49,6 +49,15 @@ where pub coords: OVector, } +impl fmt::Debug for OPoint +where + DefaultAllocator: Allocator, +{ + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { + self.coords.as_slice().fmt(formatter) + } +} + impl hash::Hash for OPoint where DefaultAllocator: Allocator, diff --git a/src/geometry/quaternion.rs b/src/geometry/quaternion.rs index 6d53863a..968a9e18 100755 --- a/src/geometry/quaternion.rs +++ b/src/geometry/quaternion.rs @@ -27,12 +27,18 @@ use crate::geometry::{Point3, Rotation}; /// A quaternion. See the type alias `UnitQuaternion = Unit` for a quaternion /// that may be used as a rotation. #[repr(C)] -#[derive(Debug, Copy, Clone)] +#[derive(Copy, Clone)] pub struct Quaternion { /// This quaternion as a 4D vector of coordinates in the `[ x, y, z, w ]` storage order. pub coords: Vector4, } +impl fmt::Debug for Quaternion { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { + self.coords.as_slice().fmt(formatter) + } +} + impl Hash for Quaternion { fn hash(&self, state: &mut H) { self.coords.hash(state) diff --git a/src/geometry/rotation.rs b/src/geometry/rotation.rs index 3ac3ca57..e1da8d67 100755 --- a/src/geometry/rotation.rs +++ b/src/geometry/rotation.rs @@ -54,11 +54,16 @@ use crate::geometry::Point; /// * [Conversion to a matrix `matrix`, `to_homogeneous`…](#conversion-to-a-matrix) /// #[repr(C)] -#[derive(Debug)] pub struct Rotation { matrix: SMatrix, } +impl fmt::Debug for Rotation { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { + self.matrix.fmt(formatter) + } +} + impl hash::Hash for Rotation where , Const>>::Buffer: hash::Hash, diff --git a/src/geometry/transform.rs b/src/geometry/transform.rs index f9dbeb51..d87a815d 100755 --- a/src/geometry/transform.rs +++ b/src/geometry/transform.rs @@ -1,6 +1,6 @@ use approx::{AbsDiffEq, RelativeEq, UlpsEq}; use std::any::Any; -use std::fmt::Debug; +use std::fmt::{self, Debug}; use std::hash; use std::marker::PhantomData; @@ -157,7 +157,6 @@ super_tcategory_impl!( /// It is stored as a matrix with dimensions `(D + 1, D + 1)`, e.g., it stores a 4x4 matrix for a /// 3D transformation. #[repr(C)] -#[derive(Debug)] pub struct Transform where Const: DimNameAdd, @@ -167,6 +166,16 @@ where _phantom: PhantomData, } +impl Debug for Transform +where + Const: DimNameAdd, + DefaultAllocator: Allocator, U1>, DimNameSum, U1>>, +{ + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { + self.matrix.fmt(formatter) + } +} + impl hash::Hash for Transform where Const: DimNameAdd, diff --git a/src/geometry/translation.rs b/src/geometry/translation.rs index 8a64b97a..48656f9c 100755 --- a/src/geometry/translation.rs +++ b/src/geometry/translation.rs @@ -22,13 +22,18 @@ use crate::geometry::Point; /// A translation. #[repr(C)] -#[derive(Debug)] pub struct Translation { /// The translation coordinates, i.e., how much is added to a point's coordinates when it is /// translated. pub vector: SVector, } +impl fmt::Debug for Translation { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { + self.vector.as_slice().fmt(formatter) + } +} + impl hash::Hash for Translation where Owned>: hash::Hash, diff --git a/tests/core/matrix.rs b/tests/core/matrix.rs index 4a35fb20..8a545e97 100644 --- a/tests/core/matrix.rs +++ b/tests/core/matrix.rs @@ -80,8 +80,8 @@ fn iter() { #[test] fn debug_output_corresponds_to_data_container() { let m = Matrix2::new(1.0, 2.0, 3.0, 4.0); - let output_stable = "Matrix { data: [[1, 3], [2, 4]] }"; // Current output on the stable channel. - let output_nightly = "Matrix { data: [[1.0, 3.0], [2.0, 4.0]] }"; // Current output on the nightly channel. + let output_stable = "[[1, 3], [2, 4]]"; // Current output on the stable channel. + let output_nightly = "[[1.0, 3.0], [2.0, 4.0]]"; // Current output on the nightly channel. let current_output = format!("{:?}", m); dbg!(output_stable); dbg!(output_nightly);