Skip to content

Add const vs. dynamic ndim to array Debug format #490

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
Oct 13, 2018
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
10 changes: 7 additions & 3 deletions src/arrayformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,13 @@ impl<'a, A: fmt::Debug, S, D: Dimension> fmt::Debug for ArrayBase<S, D>
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// Add extra information for Debug
try!(format_array(self, f, <_>::fmt));
try!(write!(f, " shape={:?}, strides={:?}, layout={:?}",
self.shape(), self.strides(), layout=self.view().layout()));
format_array(self, f, <_>::fmt)?;
write!(f, " shape={:?}, strides={:?}, layout={:?}",
self.shape(), self.strides(), layout=self.view().layout())?;
match D::NDIM {
Some(ndim) => write!(f, ", const ndim={}", ndim)?,
None => write!(f, ", dynamic ndim={}", self.ndim())?,
}
Ok(())
}
}
Expand Down
21 changes: 19 additions & 2 deletions tests/format.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

extern crate ndarray;

use ndarray::{arr0, rcarr1, aview1};
use ndarray::prelude::*;
use ndarray::rcarr1;

#[test]
fn formatting()
Expand Down Expand Up @@ -35,3 +35,20 @@ fn formatting()
let s = format!("{:02x}", aview1::<u8>(&[1, 0xff, 0xfe]));
assert_eq!(s, "[01, ff, fe]");
}

#[test]
fn debug_format() {
let a = Array2::<i32>::zeros((3, 4));
assert_eq!(
format!("{:?}", a),
"[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]] shape=[3, 4], strides=[4, 1], layout=C (0x1), const ndim=2"
);
assert_eq!(
format!("{:?}", a.into_dyn()),
"[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]] shape=[3, 4], strides=[4, 1], layout=C (0x1), dynamic ndim=2"
);
}