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.
This commit is contained in:
Benjamin Saunders 2021-09-12 10:52:46 -07:00
parent 319597e137
commit 0541f13b26
8 changed files with 51 additions and 13 deletions

View File

@ -195,10 +195,7 @@ pub struct Matrix<T, R, C, S> {
impl<T, R: Dim, C: Dim, S: fmt::Debug> fmt::Debug for Matrix<T, R, C, S> { impl<T, R: Dim, C: Dim, S: fmt::Debug> fmt::Debug for Matrix<T, R, C, S> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
formatter self.data.fmt(formatter)
.debug_struct("Matrix")
.field("data", &self.data)
.finish()
} }
} }

View File

@ -1,3 +1,4 @@
use std::fmt;
#[cfg(feature = "abomonation-serialize")] #[cfg(feature = "abomonation-serialize")]
use std::io::{Result as IOResult, Write}; use std::io::{Result as IOResult, Write};
use std::ops::Deref; 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 /// and [`UnitQuaternion`](crate::UnitQuaternion); both built on top of `Unit`. If you are interested
/// in their documentation, read their dedicated pages directly. /// in their documentation, read their dedicated pages directly.
#[repr(transparent)] #[repr(transparent)]
#[derive(Clone, Hash, Debug, Copy)] #[derive(Clone, Hash, Copy)]
pub struct Unit<T> { pub struct Unit<T> {
pub(crate) value: T, pub(crate) value: T,
} }
impl<T: fmt::Debug> fmt::Debug for Unit<T> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
self.value.fmt(formatter)
}
}
#[cfg(feature = "bytemuck")] #[cfg(feature = "bytemuck")]
unsafe impl<T> bytemuck::Zeroable for Unit<T> where T: bytemuck::Zeroable {} unsafe impl<T> bytemuck::Zeroable for Unit<T> where T: bytemuck::Zeroable {}

View File

@ -40,7 +40,7 @@ use std::mem::MaybeUninit;
/// may have some other methods, e.g., `isometry.inverse_transform_point(&point)`. See the documentation /// may have some other methods, e.g., `isometry.inverse_transform_point(&point)`. See the documentation
/// of said transformations for details. /// of said transformations for details.
#[repr(C)] #[repr(C)]
#[derive(Debug, Clone)] #[derive(Clone)]
pub struct OPoint<T: Scalar, D: DimName> pub struct OPoint<T: Scalar, D: DimName>
where where
DefaultAllocator: Allocator<T, D>, DefaultAllocator: Allocator<T, D>,
@ -49,6 +49,15 @@ where
pub coords: OVector<T, D>, pub coords: OVector<T, D>,
} }
impl<T: Scalar + fmt::Debug, D: DimName> fmt::Debug for OPoint<T, D>
where
DefaultAllocator: Allocator<T, D>,
{
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
self.coords.as_slice().fmt(formatter)
}
}
impl<T: Scalar + hash::Hash, D: DimName> hash::Hash for OPoint<T, D> impl<T: Scalar + hash::Hash, D: DimName> hash::Hash for OPoint<T, D>
where where
DefaultAllocator: Allocator<T, D>, DefaultAllocator: Allocator<T, D>,

View File

@ -27,12 +27,18 @@ use crate::geometry::{Point3, Rotation};
/// A quaternion. See the type alias `UnitQuaternion = Unit<Quaternion>` for a quaternion /// A quaternion. See the type alias `UnitQuaternion = Unit<Quaternion>` for a quaternion
/// that may be used as a rotation. /// that may be used as a rotation.
#[repr(C)] #[repr(C)]
#[derive(Debug, Copy, Clone)] #[derive(Copy, Clone)]
pub struct Quaternion<T> { pub struct Quaternion<T> {
/// This quaternion as a 4D vector of coordinates in the `[ x, y, z, w ]` storage order. /// This quaternion as a 4D vector of coordinates in the `[ x, y, z, w ]` storage order.
pub coords: Vector4<T>, pub coords: Vector4<T>,
} }
impl<T: fmt::Debug> fmt::Debug for Quaternion<T> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
self.coords.as_slice().fmt(formatter)
}
}
impl<T: Scalar + Hash> Hash for Quaternion<T> { impl<T: Scalar + Hash> Hash for Quaternion<T> {
fn hash<H: Hasher>(&self, state: &mut H) { fn hash<H: Hasher>(&self, state: &mut H) {
self.coords.hash(state) self.coords.hash(state)

View File

@ -54,11 +54,16 @@ use crate::geometry::Point;
/// * [Conversion to a matrix <span style="float:right;">`matrix`, `to_homogeneous`…</span>](#conversion-to-a-matrix) /// * [Conversion to a matrix <span style="float:right;">`matrix`, `to_homogeneous`…</span>](#conversion-to-a-matrix)
/// ///
#[repr(C)] #[repr(C)]
#[derive(Debug)]
pub struct Rotation<T, const D: usize> { pub struct Rotation<T, const D: usize> {
matrix: SMatrix<T, D, D>, matrix: SMatrix<T, D, D>,
} }
impl<T: fmt::Debug, const D: usize> fmt::Debug for Rotation<T, D> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
self.matrix.fmt(formatter)
}
}
impl<T: Scalar + hash::Hash, const D: usize> hash::Hash for Rotation<T, D> impl<T: Scalar + hash::Hash, const D: usize> hash::Hash for Rotation<T, D>
where where
<DefaultAllocator as Allocator<T, Const<D>, Const<D>>>::Buffer: hash::Hash, <DefaultAllocator as Allocator<T, Const<D>, Const<D>>>::Buffer: hash::Hash,

View File

@ -1,6 +1,6 @@
use approx::{AbsDiffEq, RelativeEq, UlpsEq}; use approx::{AbsDiffEq, RelativeEq, UlpsEq};
use std::any::Any; use std::any::Any;
use std::fmt::Debug; use std::fmt::{self, Debug};
use std::hash; use std::hash;
use std::marker::PhantomData; 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 /// It is stored as a matrix with dimensions `(D + 1, D + 1)`, e.g., it stores a 4x4 matrix for a
/// 3D transformation. /// 3D transformation.
#[repr(C)] #[repr(C)]
#[derive(Debug)]
pub struct Transform<T: RealField, C: TCategory, const D: usize> pub struct Transform<T: RealField, C: TCategory, const D: usize>
where where
Const<D>: DimNameAdd<U1>, Const<D>: DimNameAdd<U1>,
@ -167,6 +166,16 @@ where
_phantom: PhantomData<C>, _phantom: PhantomData<C>,
} }
impl<T: RealField + Debug, C: TCategory, const D: usize> Debug for Transform<T, C, D>
where
Const<D>: DimNameAdd<U1>,
DefaultAllocator: Allocator<T, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>,
{
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
self.matrix.fmt(formatter)
}
}
impl<T: RealField + hash::Hash, C: TCategory, const D: usize> hash::Hash for Transform<T, C, D> impl<T: RealField + hash::Hash, C: TCategory, const D: usize> hash::Hash for Transform<T, C, D>
where where
Const<D>: DimNameAdd<U1>, Const<D>: DimNameAdd<U1>,

View File

@ -22,13 +22,18 @@ use crate::geometry::Point;
/// A translation. /// A translation.
#[repr(C)] #[repr(C)]
#[derive(Debug)]
pub struct Translation<T, const D: usize> { pub struct Translation<T, const D: usize> {
/// The translation coordinates, i.e., how much is added to a point's coordinates when it is /// The translation coordinates, i.e., how much is added to a point's coordinates when it is
/// translated. /// translated.
pub vector: SVector<T, D>, pub vector: SVector<T, D>,
} }
impl<T: fmt::Debug, const D: usize> fmt::Debug for Translation<T, D> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
self.vector.as_slice().fmt(formatter)
}
}
impl<T: Scalar + hash::Hash, const D: usize> hash::Hash for Translation<T, D> impl<T: Scalar + hash::Hash, const D: usize> hash::Hash for Translation<T, D>
where where
Owned<T, Const<D>>: hash::Hash, Owned<T, Const<D>>: hash::Hash,

View File

@ -80,8 +80,8 @@ fn iter() {
#[test] #[test]
fn debug_output_corresponds_to_data_container() { fn debug_output_corresponds_to_data_container() {
let m = Matrix2::new(1.0, 2.0, 3.0, 4.0); 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_stable = "[[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_nightly = "[[1.0, 3.0], [2.0, 4.0]]"; // Current output on the nightly channel.
let current_output = format!("{:?}", m); let current_output = format!("{:?}", m);
dbg!(output_stable); dbg!(output_stable);
dbg!(output_nightly); dbg!(output_nightly);