slice_utils/
debug.rs

1use core::fmt;
2
3use crate::{
4    ArrayWindowsBorrowed, ArrayWindowsOwned, Chain, Cycle, FromFn, Interleave, MapBorrowed,
5    MapOwned, Reverse, Slice, SliceBorrowed, SliceOf, SliceOwned, SplitMut, WindowsBorrowed,
6    WindowsOwned, Zip,
7};
8
9macro_rules! impl_debug {
10    ($(
11        $typ:ident [$($lt:lifetime),* $($generics:ident),*]
12    ;)*) => {$(
13        impl<
14            $($lt,)* T, S $(, $generics)*,
15        > fmt::Debug for $typ<$($lt,)* S $(, $generics)*>
16        where
17            T: fmt::Debug,
18            S: Slice<Output = T>,
19            $( $generics: Slice<Output = T>,)*
20        {
21            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
22                let mut list = f.debug_list();
23                for i in 0..self.len() {
24                    self.get_with(i, &mut |x| { list.entry(x); });
25                }
26                list.finish()
27            }
28        }
29    )*};
30}
31
32impl_debug! {
33    Chain[S2];
34    Interleave[S2];
35    Reverse[];
36    SliceOf[];
37    SplitMut['a];
38}
39
40// Separate impl to avoid infinite debug printing
41impl<T, S> fmt::Debug for Cycle<S>
42where
43    T: fmt::Debug,
44    S: Slice<Output = T>,
45{
46    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
47        struct Ellipses;
48        impl fmt::Debug for Ellipses {
49            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50                write!(f, "...")
51            }
52        }
53
54        let mut list = f.debug_list();
55        for i in 0..self.0.len() {
56            self.get_with(i, &mut |x| {
57                list.entry(x);
58            });
59        }
60        list.entry(&Ellipses);
61        list.finish()
62    }
63}
64
65impl<T, S, F, U> fmt::Debug for MapOwned<S, F>
66where
67    S: SliceOwned<Output = T>,
68    F: Fn(T) -> U,
69    U: fmt::Debug,
70{
71    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
72        let mut list = f.debug_list();
73        for i in 0..self.len() {
74            self.get_with(i, &mut |x| {
75                list.entry(x);
76            });
77        }
78        list.finish()
79    }
80}
81
82impl<T, S, F, U> fmt::Debug for MapBorrowed<S, F>
83where
84    S: SliceBorrowed<Output = T>,
85    F: Fn(&T) -> U,
86    U: fmt::Debug,
87{
88    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
89        let mut list = f.debug_list();
90        for i in 0..self.len() {
91            self.get_with(i, &mut |x| {
92                list.entry(x);
93            });
94        }
95        list.finish()
96    }
97}
98
99impl<F, T> fmt::Debug for FromFn<F>
100where
101    F: Fn(usize) -> Option<T>,
102    T: fmt::Debug,
103{
104    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
105        let mut list = f.debug_list();
106        for i in 0..self.len() {
107            self.get_with(i, &mut |x| {
108                list.entry(x);
109            });
110        }
111        list.finish()
112    }
113}
114
115impl<T, S1, S2> fmt::Debug for Zip<S1, S2>
116where
117    T: fmt::Debug,
118    S1: SliceOwned<Output = T>,
119    S2: SliceOwned<Output = T>,
120{
121    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
122        let mut list = f.debug_list();
123        for i in 0..self.len() {
124            self.get_with(i, &mut |x| {
125                list.entry(x);
126            });
127        }
128        list.finish()
129    }
130}
131
132impl<'a, S, T> fmt::Debug for WindowsOwned<'a, S>
133where
134    S: SliceOwned<Output = T>,
135    T: fmt::Debug,
136{
137    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
138        let mut list = f.debug_list();
139        for i in 0..Slice::len(self) {
140            self.get_with(i, &mut |x| {
141                list.entry(x);
142            });
143        }
144        list.finish()
145    }
146}
147
148impl<'a, S, T> fmt::Debug for WindowsBorrowed<'a, S>
149where
150    S: SliceBorrowed<Output = T>,
151    T: fmt::Debug,
152{
153    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
154        let mut list = f.debug_list();
155        for i in 0..Slice::len(self) {
156            self.get_with(i, &mut |x| {
157                list.entry(x);
158            });
159        }
160        list.finish()
161    }
162}
163
164impl<S, T, const N: usize> fmt::Debug for ArrayWindowsOwned<S, N>
165where
166    S: SliceOwned<Output = T>,
167    T: fmt::Debug,
168{
169    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
170        let mut list = f.debug_list();
171        for i in 0..Slice::len(self) {
172            self.get_with(i, &mut |x| {
173                list.entry(x);
174            });
175        }
176        list.finish()
177    }
178}
179
180impl<'a, S, T, const N: usize> fmt::Debug for ArrayWindowsBorrowed<'a, S, N>
181where
182    S: SliceBorrowed<Output = T>,
183    T: fmt::Debug,
184{
185    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
186        let mut list = f.debug_list();
187        for i in 0..Slice::len(self) {
188            self.get_with(i, &mut |x| {
189                list.entry(x);
190            });
191        }
192        list.finish()
193    }
194}