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
129
130
131
132
133
134
135
136
use crate::{Slice, SliceBorrowed, SliceOf, SliceOwned};

macro_rules! either {
    ( [[] $($_:tt)*][$($t:tt)*] ) => {$($t)*};
    ( [[$($_:tt)*] $($t:tt)*][$($__:tt)*] ) => {$($t)*};
}

macro_rules! def_window {
    ( $owned:ident, [$($lt:lifetime)?], $sized:ty, $item:ty, $fn:ident ) => {
        paste::paste! {
            /// An iterator over overlapping chunks of a slice; see
            #[doc = concat!("[`Slice", stringify!($owned), "::chunks`].")]
            #[derive(Debug, Clone, Copy)]
            pub struct [<Chunks $owned>]<'a, S: ?Sized> {
                /// The slice underlying the iterator.
                pub data: &'a S,
                size: usize,
                i: usize,
            }

            impl<'a, S> [<Chunks $owned>]<'a, S>
            where
                S: [<Slice $owned>] + ?Sized,
            {
                /// Create a new iterator; see
                #[doc = concat!("[`Slice", stringify!($owned), "::chunks`].")]
                pub fn new(data: &'a S, size: usize) -> Self {
                    if size == 0 {
                        panic!("cannot call `chunks` with size = 0");
                    }

                    Self {
                        data,
                        size,
                        i: 0,
                    }
                }
            }

            impl<'a, S> Iterator for [<Chunks $owned>]<'a, S>
            where
                S: [<Slice $owned>] + ?Sized,
            {
                type Item = SliceOf<&'a S>;

                fn next(&mut self) -> Option<Self::Item> {
                    if self.i == self.data.len() {
                        None
                    } else {
                        let start = self.i;
                        self.i = self.data.len().min(self.i + self.size);

                        self.data.slice(start..self.i)
                    }
                }

                fn size_hint(&self) -> (usize, Option<usize>) {
                    let len = self.data.len() / self.size;
                    (len, Some(len))
                }
            }

            impl<'a, S> ExactSizeIterator
                for [<Chunks $owned>]<'a, S>
            where
                S: [<Slice $owned>] + ?Sized {}

            type [<ArrayWindow $owned>]<$($lt,)? S> = either!([[$($lt)?] $(&$lt S)?][S]);
            /// An iterator over overlapping chunks of a slice; see
            #[doc = concat!("[`Slice", stringify!($owned), "::array_chunks`].")]
            #[derive(Debug, Clone, Copy)]
            pub struct [<ArrayChunks $owned>]<$($lt,)? S: $sized, const N: usize> {
                /// The slice underlying the iterator.
                pub data: [<ArrayWindow $owned>]<$($lt,)? S>,
                i: usize,
            }

            impl<$($lt,)? S, const N: usize> [<ArrayChunks $owned>]<$($lt,)? S, N>
            where
                S: [<Slice $owned>] + $sized,
            {
                /// Create a new iterator; see
                #[doc = concat!("[`Slice", stringify!($owned), "::array_chunks`].")]
                pub fn new(data: either!([[$($lt)?] $(&$lt S)?][S])) -> Self {
                    // TODO: make this a comptime assertion
                    if N == 0 {
                        panic!("cannot call `chunks` with size = 0");
                    }

                    Self {
                        data,
                        i: 0,
                    }
                }

                /// Returns the leftover from the end of the slice, if any.
                pub fn remainder(&self) -> SliceOf<&S> {
                    let len = self.data.len();
                    let start = len - (len % N);
                    (either!([[$($lt)?] self.data][&self.data])).slice(start..).unwrap()
                }
            }

            impl<$($lt,)? S, const N: usize> Iterator for [<ArrayChunks $owned>]<$($lt,)? S, N>
            where
                S: [<Slice $owned>] + $sized,
            {
                type Item = [$item; N];

                fn next(&mut self) -> Option<Self::Item> {
                    if self.i + N > self.data.len() {
                        None
                    } else {
                        let start = self.i;
                        self.i += N;

                        Some(core::array::from_fn(|i| self.data.$fn(start + i).unwrap()))
                    }
                }

                fn size_hint(&self) -> (usize, Option<usize>) {
                    let len = self.data.len() / N;
                    (len, Some(len))
                }
            }

            impl<$($lt,)? S, const N: usize> ExactSizeIterator
                for [<ArrayChunks $owned>]<$($lt,)? S, N>
            where
                S: [<Slice $owned>] + $sized {}
        }
    };
}

def_window!(Owned, [], Sized, S::Output, get_owned);
def_window!(Borrowed, ['a], ?Sized, &'a S::Output, get);