Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
make Display on matrices respect formatting params
  • Loading branch information
martin-t committed Jul 31, 2020
1 parent 5ab4c93 commit 782bcfe
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions src/mat.rs
Expand Up @@ -377,12 +377,21 @@ macro_rules! mat_impl_mat {
impl<T: Display> Display for $Mat<T> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "(")?;
for row in &self.rows {
let mut rows = self.rows.iter();
// first row goes after the opening paren
if let Some(row) = rows.next(){
for elem in row {
write!(f, " {}", elem)?;
write!(f, " ")?;
elem.fmt(f)?;
}
}
// subsequent rows start on a new line
for row in rows{
write!(f, "\n ")?;
for elem in row {
write!(f, " ")?;
elem.fmt(f)?;
}
writeln!(f, "")?;
write!(f, " ")?;
}
write!(f, " )")
}
Expand Down Expand Up @@ -897,15 +906,24 @@ macro_rules! mat_impl_mat {
impl<T: Display> Display for $Mat<T> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "(")?;
for y in 0..$nrows {
// first row goes after the opening paren
for x in 0..$ncols {
write!(f, " ")?;
let elem = unsafe {
self.cols.get_unchecked(x).get_unchecked(0)
};
elem.fmt(f)?;
}
// subsequent rows start on a new line
for y in 1..$nrows {
write!(f, "\n ")?;
for x in 0..$ncols {
write!(f, " ")?;
let elem = unsafe {
self.cols.get_unchecked(x).get_unchecked(y)
};
write!(f, " {}", elem)?;
elem.fmt(f)?;
}
writeln!(f, "")?;
write!(f, " ")?;
}
write!(f, " )")
}
Expand Down

0 comments on commit 782bcfe

Please sign in to comment.