Skip to content

Commit a6db082

Browse files
authored
Make Debug instances behave like those for slices (#211)
The indentantion in case of structs inside the arrays was wrong with pretty debug instance. Instead of rolling one by hand, just use the underlying slice implementation. It appears that this was even the intention orginally, based on the `TinyVec_pretty_debug` test. The main issue is that the test used a simple value instead of a struct so it missed the problem. FWIW I have kept the existing test but I think it could just be deleted now. I haven't touched the `Display` version because while slices have a `Debug` impl, there isn't one for `Display`. So I think probably `Display` has the same problem. These tests can technically be written in a non-alloc/non-std way by defining an array-backed buffer and giving it `core::fmt::Write` but I was too lazy to do so: it doesn't seem useful. Fixes #192.
1 parent 3c613ad commit a6db082

4 files changed

Lines changed: 74 additions & 42 deletions

File tree

src/arrayvec.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,20 +1712,7 @@ where
17121712
{
17131713
#[allow(clippy::missing_inline_in_public_items)]
17141714
fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
1715-
write!(f, "[")?;
1716-
if f.alternate() && !self.is_empty() {
1717-
write!(f, "\n ")?;
1718-
}
1719-
for (i, elem) in self.iter().enumerate() {
1720-
if i > 0 {
1721-
write!(f, ",{}", if f.alternate() { "\n " } else { " " })?;
1722-
}
1723-
Debug::fmt(elem, f)?;
1724-
}
1725-
if f.alternate() && !self.is_empty() {
1726-
write!(f, ",\n")?;
1727-
}
1728-
write!(f, "]")
1715+
<[A::Item] as Debug>::fmt(self.as_slice(), f)
17291716
}
17301717
}
17311718

@@ -2056,4 +2043,26 @@ mod test {
20562043
av.retain_mut(|&mut x| x % 2 == 0);
20572044
assert_eq!(av.len(), 0);
20582045
}
2046+
2047+
#[cfg(feature = "alloc")]
2048+
#[test]
2049+
fn array_like_debug() {
2050+
#[derive(Debug, Default, Copy, Clone)]
2051+
struct S {
2052+
x: u8,
2053+
y: u8,
2054+
}
2055+
2056+
use core::fmt::Write;
2057+
2058+
let mut ar: [S; 2] = [S { x: 1, y: 2 }, S { x: 3, y: 4 }];
2059+
let mut buf_ar = alloc::string::String::new();
2060+
write!(&mut buf_ar, "{ar:#?}");
2061+
2062+
let av: ArrayVec<[S; 2]> = ArrayVec::from(ar);
2063+
let mut buf_av = alloc::string::String::new();
2064+
write!(&mut buf_av, "{av:#?}");
2065+
2066+
assert_eq!(buf_av, buf_ar)
2067+
}
20592068
}

src/slicevec.rs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -890,20 +890,7 @@ where
890890
{
891891
#[allow(clippy::missing_inline_in_public_items)]
892892
fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
893-
write!(f, "[")?;
894-
if f.alternate() && !self.is_empty() {
895-
write!(f, "\n ")?;
896-
}
897-
for (i, elem) in self.iter().enumerate() {
898-
if i > 0 {
899-
write!(f, ",{}", if f.alternate() { "\n " } else { " " })?;
900-
}
901-
Debug::fmt(elem, f)?;
902-
}
903-
if f.alternate() && !self.is_empty() {
904-
write!(f, ",\n")?;
905-
}
906-
write!(f, "]")
893+
<[T] as Debug>::fmt(self.as_slice(), f)
907894
}
908895
}
909896

@@ -1067,3 +1054,30 @@ where
10671054
write!(f, "]")
10681055
}
10691056
}
1057+
1058+
#[cfg(test)]
1059+
mod test {
1060+
use super::*;
1061+
1062+
#[cfg(feature = "alloc")]
1063+
#[test]
1064+
fn array_like_debug() {
1065+
#[derive(Debug, Default, Copy, Clone)]
1066+
struct S {
1067+
x: u8,
1068+
y: u8,
1069+
}
1070+
1071+
use core::fmt::Write;
1072+
1073+
let mut ar: [S; 2] = [S { x: 1, y: 2 }, S { x: 3, y: 4 }];
1074+
let mut buf_ar = alloc::string::String::new();
1075+
write!(&mut buf_ar, "{ar:#?}");
1076+
1077+
let av: SliceVec<S> = SliceVec::from(&mut ar);
1078+
let mut buf_av = alloc::string::String::new();
1079+
write!(&mut buf_av, "{av:#?}");
1080+
1081+
assert_eq!(buf_av, buf_ar)
1082+
}
1083+
}

src/tinyvec.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,20 +1676,7 @@ where
16761676
{
16771677
#[allow(clippy::missing_inline_in_public_items)]
16781678
fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
1679-
write!(f, "[")?;
1680-
if f.alternate() && !self.is_empty() {
1681-
write!(f, "\n ")?;
1682-
}
1683-
for (i, elem) in self.iter().enumerate() {
1684-
if i > 0 {
1685-
write!(f, ",{}", if f.alternate() { "\n " } else { " " })?;
1686-
}
1687-
Debug::fmt(elem, f)?;
1688-
}
1689-
if f.alternate() && !self.is_empty() {
1690-
write!(f, ",\n")?;
1691-
}
1692-
write!(f, "]")
1679+
<[A::Item] as Debug>::fmt(self.as_slice(), f)
16931680
}
16941681
}
16951682

tests/tinyvec.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,3 +499,25 @@ fn TinyVec_std_io_write() {
499499
assert!(tv.is_heap());
500500
assert_eq!(tv, tiny_vec![b'f', b'o', b'o', b'b', b'a', b'r']);
501501
}
502+
503+
#[cfg(feature = "alloc")]
504+
#[test]
505+
fn TinyVec_array_like_debug() {
506+
#[derive(Debug, Default, Copy, Clone)]
507+
struct S {
508+
x: u8,
509+
y: u8,
510+
}
511+
512+
use core::fmt::Write;
513+
514+
let mut ar: [S; 2] = [S { x: 1, y: 2 }, S { x: 3, y: 4 }];
515+
let mut buf_ar = alloc::string::String::new();
516+
write!(&mut buf_ar, "{ar:#?}");
517+
518+
let av: TinyVec<[S; 2]> = TinyVec::from(ar);
519+
let mut buf_av = alloc::string::String::new();
520+
write!(&mut buf_av, "{av:#?}");
521+
522+
assert_eq!(buf_av, buf_ar)
523+
}

0 commit comments

Comments
 (0)