Skip to content

Commit d5224a2

Browse files
committed
Add const vs. dynamic ndim to array Debug format
The difference between const-dimensional and dynamic-dimensional arrays is fairly significant, so it is useful for the debug format to include this information.
1 parent a175675 commit d5224a2

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/arrayformat.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,13 @@ impl<'a, A: fmt::Debug, S, D: Dimension> fmt::Debug for ArrayBase<S, D>
105105
{
106106
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
107107
// Add extra information for Debug
108-
try!(format_array(self, f, <_>::fmt));
109-
try!(write!(f, " shape={:?}, strides={:?}, layout={:?}",
110-
self.shape(), self.strides(), layout=self.view().layout()));
108+
format_array(self, f, <_>::fmt)?;
109+
write!(f, " shape={:?}, strides={:?}, layout={:?}",
110+
self.shape(), self.strides(), layout=self.view().layout())?;
111+
match D::NDIM {
112+
Some(ndim) => write!(f, ", const ndim={}", ndim)?,
113+
None => write!(f, ", dynamic ndim={}", self.ndim())?,
114+
}
111115
Ok(())
112116
}
113117
}

tests/format.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
21
extern crate ndarray;
32

4-
use ndarray::{arr0, rcarr1, aview1};
3+
use ndarray::prelude::*;
4+
use ndarray::rcarr1;
55

66
#[test]
77
fn formatting()
@@ -35,3 +35,20 @@ fn formatting()
3535
let s = format!("{:02x}", aview1::<u8>(&[1, 0xff, 0xfe]));
3636
assert_eq!(s, "[01, ff, fe]");
3737
}
38+
39+
#[test]
40+
fn debug_format() {
41+
let a = Array2::<i32>::zeros((3, 4));
42+
assert_eq!(
43+
format!("{:?}", a),
44+
"[[0, 0, 0, 0],
45+
[0, 0, 0, 0],
46+
[0, 0, 0, 0]] shape=[3, 4], strides=[4, 1], layout=C (0x1), const ndim=2"
47+
);
48+
assert_eq!(
49+
format!("{:?}", a.into_dyn()),
50+
"[[0, 0, 0, 0],
51+
[0, 0, 0, 0],
52+
[0, 0, 0, 0]] shape=[3, 4], strides=[4, 1], layout=C (0x1), dynamic ndim=2"
53+
);
54+
}

0 commit comments

Comments
 (0)