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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use core::fmt;

use crate::{Lender, Lending, Peekable};

#[derive(Clone)]
#[must_use = "lenders are lazy and do nothing unless consumed"]
pub struct Intersperse<'this, L>
where
    for<'all> <L as Lending<'all>>::Lend: Clone,
    L: Lender,
{
    lender: Peekable<'this, L>,
    separator: <L as Lending<'this>>::Lend,
    needs_sep: bool,
}
impl<'this, L> Intersperse<'this, L>
where
    for<'all> <L as Lending<'all>>::Lend: Clone,
    L: Lender,
{
    pub(crate) fn new(lender: L, separator: <L as Lending<'this>>::Lend) -> Self {
        Self { lender: lender.peekable(), separator, needs_sep: false }
    }
}
impl<'this, L: fmt::Debug> fmt::Debug for Intersperse<'this, L>
where
    for<'all> <L as Lending<'all>>::Lend: Clone + fmt::Debug,
    L: Lender,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Intersperse")
            .field("lender", &self.lender)
            .field("separator", &self.separator)
            .field("needs_sep", &self.needs_sep)
            .finish()
    }
}
impl<'lend, 'this, L> Lending<'lend> for Intersperse<'this, L>
where
    for<'all> <L as Lending<'all>>::Lend: Clone,
    L: Lender,
{
    type Lend = <L as Lending<'lend>>::Lend;
}
impl<'this, L> Lender for Intersperse<'this, L>
where
    for<'all> <L as Lending<'all>>::Lend: Clone,
    L: Lender,
{
    fn next<'next>(&'next mut self) -> Option<<Self as Lending<'next>>::Lend> {
        if self.needs_sep && self.lender.peek().is_some() {
            self.needs_sep = false;
            // SAFETY: 'this: 'next
            Some(unsafe {
                core::mem::transmute::<<Self as Lending<'this>>::Lend, <Self as Lending<'next>>::Lend>(
                    self.separator.clone(),
                )
            })
        } else {
            self.needs_sep = true;
            self.lender.next()
        }
    }
    fn fold<B, F>(mut self, init: B, mut f: F) -> B
    where
        Self: Sized,
        F: FnMut(B, <Self as Lending<'_>>::Lend) -> B,
    {
        let mut acc = init;
        if !self.needs_sep {
            if let Some(x) = self.lender.next() {
                acc = f(acc, x);
            } else {
                return acc;
            }
        }
        self.lender.fold(acc, |mut acc, x| {
            acc = f(acc, self.separator.clone());
            acc = f(acc, x);
            acc
        })
    }
    fn size_hint(&self) -> (usize, Option<usize>) { intersperse_size_hint(&self.lender, self.needs_sep) }
}

#[derive(Clone)]
#[must_use = "lenders are lazy and do nothing unless consumed"]
pub struct IntersperseWith<'this, L, G>
where
    L: Lender,
{
    separator: G,
    lender: Peekable<'this, L>,
    needs_sep: bool,
}
impl<'this, L, G> IntersperseWith<'this, L, G>
where
    L: Lender,
    G: FnMut() -> <L as Lending<'this>>::Lend,
{
    pub(crate) fn new(lender: L, seperator: G) -> Self {
        Self { lender: Peekable::new(lender), separator: seperator, needs_sep: false }
    }
}
impl<'this, L: fmt::Debug, G: fmt::Debug> fmt::Debug for IntersperseWith<'this, L, G>
where
    L: Lender,
    for<'all> <L as Lending<'all>>::Lend: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("IntersperseWith")
            .field("lender", &self.lender)
            .field("separator", &self.separator)
            .field("needs_sep", &self.needs_sep)
            .finish()
    }
}
impl<'lend, 'this, L, G> Lending<'lend> for IntersperseWith<'this, L, G>
where
    L: Lender,
    G: FnMut() -> <L as Lending<'this>>::Lend,
{
    type Lend = <L as Lending<'lend>>::Lend;
}
impl<'this, L, G> Lender for IntersperseWith<'this, L, G>
where
    L: Lender,
    G: FnMut() -> <L as Lending<'this>>::Lend,
{
    fn next<'next>(&'next mut self) -> Option<<Self as Lending<'next>>::Lend> {
        if self.needs_sep && self.lender.peek().is_some() {
            self.needs_sep = false;
            // SAFETY: 'this: 'next
            Some(unsafe {
                core::mem::transmute::<<Self as Lending<'this>>::Lend, <Self as Lending<'next>>::Lend>((self.separator)())
            })
        } else {
            self.needs_sep = true;
            self.lender.next()
        }
    }
    fn fold<B, F>(mut self, init: B, mut f: F) -> B
    where
        Self: Sized,
        F: FnMut(B, <Self as Lending<'_>>::Lend) -> B,
    {
        let mut acc = init;
        if !self.needs_sep {
            if let Some(x) = self.lender.next() {
                acc = f(acc, x);
            } else {
                return acc;
            }
        }
        self.lender.fold(acc, |mut acc, x| {
            acc = f(acc, (self.separator)());
            acc = f(acc, x);
            acc
        })
    }
    fn size_hint(&self) -> (usize, Option<usize>) { intersperse_size_hint(&self.lender, self.needs_sep) }
}

fn intersperse_size_hint<L>(lender: &L, needs_sep: bool) -> (usize, Option<usize>)
where
    L: Lender,
{
    let (lo, hi) = lender.size_hint();
    let next_is_elem = !needs_sep;
    (
        lo.saturating_sub(next_is_elem as usize).saturating_add(lo),
        hi.and_then(|hi| hi.saturating_sub(next_is_elem as usize).checked_add(hi)),
    )
}