1use std::fmt::{Debug, Display, Formatter};
2
3pub struct DisplayWrapper<'i, T> {
4 inner: &'i T,
5}
6
7impl<T: Display> Debug for DisplayWrapper<'_, T> {
8 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
9 Display::fmt(&self.inner, f)
10 }
11}
12
13impl<'i, T> DisplayWrapper<'i, T> {
14 pub fn new(inner: &'i T) -> Self {
15 Self { inner }
16 }
17}
18
19pub struct DisplayList<'i, T> {
20 inner: &'i [T],
21}
22
23impl<T: Display> Debug for DisplayList<'_, T> {
24 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
25 f.debug_list().entries(self.inner.iter().map(|x| DisplayWrapper::new(x))).finish()
26 }
27}
28
29impl<'i, T> DisplayList<'i, T> {
30 pub fn new(inner: &'i [T]) -> Self {
31 Self { inner }
32 }
33}
34
35pub struct DisplayMap<K, V> {
36 inner: Vec<(K, V)>,
37}
38
39impl<K, V> Debug for DisplayMap<K, V>
40where
41 K: Display,
42 V: Display,
43{
44 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
45 f.debug_map().entries(self.inner.iter().map(|(k, v)| (DisplayWrapper::new(k), DisplayWrapper::new(v)))).finish()
46 }
47}
48
49impl<K, V> FromIterator<(K, V)> for DisplayMap<K, V> {
50 fn from_iter<T>(iter: T) -> Self
51 where
52 T: IntoIterator<Item = (K, V)>,
53 {
54 Self { inner: iter.into_iter().collect() }
55 }
56}