From 831142d618b3d263907d2499509957f291ecfc04 Mon Sep 17 00:00:00 2001 From: Owen Brooks Date: Wed, 15 Jun 2022 13:42:14 +1000 Subject: [PATCH] Make matrix Debug output semicolon-delimited rows --- src/base/matrix.rs | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/src/base/matrix.rs b/src/base/matrix.rs index 8f8786c1..9afd8a14 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -195,9 +195,47 @@ pub struct Matrix { _phantoms: PhantomData<(T, R, C)>, } -impl fmt::Debug for Matrix { - fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { - self.data.fmt(formatter) +impl + fmt::Debug> fmt::Debug + for Matrix +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { + write!(f, "Matrix {{ ")?; + if f.alternate() { + write!(f, "\n ")?; + } + write!(f, "data: [")?; + let nrows = self.nrows(); + let ncols = self.ncols(); + for i in 0..nrows { + for j in 0..ncols { + if j != 0 { + write!(f, ", ")?; + } + // Safety: the indices are within range + unsafe { + (*self.data.get_unchecked(i, j)).fmt(f)?; + } + } + if i != nrows - 1 { + write!(f, "; ")?; + } + if f.alternate() && i != nrows - 1 { + write!(f, "\n ")?; + } + } + write!(f, "], ")?; + if f.alternate() { + write!(f, "\n ")?; + } + write!(f, "nrows: {:?}, ", nrows)?; + if f.alternate() { + write!(f, "\n ")?; + } + write!(f, "ncols: {:?} ", ncols)?; + if f.alternate() { + writeln!(f, "")?; + } + write!(f, "}}") } }