iterable/lazy/
lazy_chain.rs

1use crate::{Consumer, Iterable, IterableSeq};
2
3#[must_use = "iterable adaptors are lazy and do nothing unless consumed"]
4#[derive(Debug, Clone)]
5pub struct LazyChain<I, C> {
6    pub(crate) iterable: I,
7    pub(crate) c: C,
8}
9
10impl<I, C> Iterable for LazyChain<I, C>
11where
12    I: Iterable,
13    C: Consumer<Item = I::Item>,
14{
15    type C = I::C;
16    type CC<U> = I::CC<U>;
17    // remove below after `associated_type_defaults` stabilized
18    type F = I::C;
19    type CF<U> = I::CC<U>;
20}
21
22impl<I, C> IterableSeq for LazyChain<I, C>
23where
24    I: IterableSeq,
25    C: Consumer<Item = I::Item>,
26{
27}
28
29impl<I, C> Consumer for LazyChain<I, C>
30where
31    I: Consumer,
32    C: Consumer<Item = I::Item>,
33{
34    type Item = I::Item;
35    type IntoIter = std::iter::Chain<I::IntoIter, C::IntoIter>;
36    fn consume(self) -> Self::IntoIter {
37        self.iterable.consume().chain(self.c.consume())
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44    use crate::lazy::collect;
45
46    #[test]
47    fn smoke() {
48        let v = vec![1, 2, 3];
49        let s = vec![4, 5, 6];
50        let res = collect(v.lazy_chain(s));
51        assert_eq!(res, vec![1, 2, 3, 4, 5, 6]);
52    }
53}