Skip to content

Commit 83c9f08

Browse files
author
LukeMathWalker
committed
Use custom enum
1 parent e049abd commit 83c9f08

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/arrayformat.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,34 @@ fn format_1d_array<A, S, F>(
3434
write!(f, "[")?;
3535
for (j, index) in to_be_printed.into_iter().enumerate() {
3636
match index {
37-
Some(i) => {
37+
PrintableCell::ElementIndex(i) => {
3838
format(&view[i], f)?;
3939
if j != n_to_be_printed - 1 {
4040
write!(f, ", ")?;
4141
}
4242
},
43-
None => write!(f, "..., ")?,
43+
PrintableCell::Ellipses => write!(f, "..., ")?,
4444
}
4545
}
4646
write!(f, "]")?;
4747
Ok(())
4848
}
4949

50+
enum PrintableCell {
51+
ElementIndex(usize),
52+
Ellipses,
53+
}
54+
5055
// Returns what indexes should be printed for a certain axis.
51-
// If the axis is longer than 2 * limit, a `None` is inserted
56+
// If the axis is longer than 2 * limit, a `Ellipses` is inserted
5257
// where indexes are being omitted.
53-
fn to_be_printed(length: usize, limit: usize) -> Vec<Option<usize>> {
58+
fn to_be_printed(length: usize, limit: usize) -> Vec<PrintableCell> {
5459
if length <= 2 * limit {
55-
(0..length).map(|x| Some(x)).collect()
60+
(0..length).map(|x| PrintableCell::ElementIndex(x)).collect()
5661
} else {
57-
let mut v: Vec<Option<usize>> = (0..limit).map(|x| Some(x)).collect();
58-
v.push(None);
59-
v.extend((length-limit..length).map(|x| Some(x)));
62+
let mut v: Vec<PrintableCell> = (0..limit).map(|x| PrintableCell::ElementIndex(x)).collect();
63+
v.push(PrintableCell::Ellipses);
64+
v.extend((length-limit..length).map(|x| PrintableCell::ElementIndex(x)));
6065
v
6166
}
6267
}
@@ -96,7 +101,7 @@ where
96101
write!(f, "[")?;
97102
for (j, index) in to_be_printed.into_iter().enumerate() {
98103
match index {
99-
Some(i) => {
104+
PrintableCell::ElementIndex(i) => {
100105
// Proceed recursively with the (n-1)-dimensional slice
101106
format_array(
102107
&view.index_axis(Axis(0), i), f, format.clone(), limit
@@ -107,7 +112,7 @@ where
107112
write!(f, ",\n ")?
108113
}
109114
},
110-
None => write!(f, "...,\n ")?
115+
PrintableCell::Ellipses => write!(f, "...,\n ")?
111116
}
112117
}
113118
write!(f, "]")?;

0 commit comments

Comments
 (0)