1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use core::fmt;

use crate::{
    Chain, Cycle, FromFn, Interleave, MapBorrowed, MapOwned, Reverse, Slice, SliceBorrowed,
    SliceOf, SliceOwned, Zip,
};

macro_rules! impl_debug {
    ($(
        $typ:ident [$($generics:ident),*]
    ;)*) => {$(
        impl<
            T, S $(, $generics)*,
        > fmt::Debug for $typ<S $(, $generics)*>
        where
            T: fmt::Debug,
            S: Slice<Output = T>,
            $( $generics: Slice<Output = T>,)*
        {
            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                let mut list = f.debug_list();
                for i in 0..self.len() {
                    self.get_with(i, &mut |x| { list.entry(x); });
                }
                list.finish()
            }
        }
    )*};
}

impl_debug! {
    Chain[S2];
    Interleave[S2];
    Reverse[];
    SliceOf[];
}

// Separate impl to avoid infinite debug printing
impl<T, S> fmt::Debug for Cycle<S>
where
    T: fmt::Debug,
    S: Slice<Output = T>,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        struct Ellipses;
        impl fmt::Debug for Ellipses {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                write!(f, "...")
            }
        }

        let mut list = f.debug_list();
        for i in 0..self.0.len() {
            self.get_with(i, &mut |x| {
                list.entry(x);
            });
        }
        list.entry(&Ellipses);
        list.finish()
    }
}

impl<T, S, F, U> fmt::Debug for MapOwned<S, F>
where
    S: SliceOwned<Output = T>,
    F: Fn(T) -> U,
    U: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut list = f.debug_list();
        for i in 0..self.len() {
            self.get_with(i, &mut |x| {
                list.entry(x);
            });
        }
        list.finish()
    }
}

impl<T, S, F, U> fmt::Debug for MapBorrowed<S, F>
where
    S: SliceBorrowed<Output = T>,
    F: Fn(&T) -> U,
    U: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut list = f.debug_list();
        for i in 0..self.len() {
            self.get_with(i, &mut |x| {
                list.entry(x);
            });
        }
        list.finish()
    }
}

impl<F, T> fmt::Debug for FromFn<F>
where
    F: Fn(usize) -> Option<T>,
    T: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut list = f.debug_list();
        for i in 0..self.len() {
            self.get_with(i, &mut |x| {
                list.entry(x);
            });
        }
        list.finish()
    }
}

impl<T, S1, S2> fmt::Debug for Zip<S1, S2>
where
    T: fmt::Debug,
    S1: SliceOwned<Output = T>,
    S2: SliceOwned<Output = T>,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut list = f.debug_list();
        for i in 0..self.len() {
            self.get_with(i, &mut |x| {
                list.entry(x);
            });
        }
        list.finish()
    }
}