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
use crate::{Windows, WindowsMut};

/// An extension trait for iterators that allows turning them into lending iterators (over windows of elements).
pub trait ToLendingIterator: Iterator {
    /// Turns this iterator into a lending iterator over windows of elements (&[Item]).
    ///
    /// `Windows` is backed by a buffer that grows to at most size * 2.
    /// This was chosen as a compromise between memory usage and time complexity:
    /// if the buffer was limited to size `size`, we would need to shift all the elements
    /// on every iteration.
    fn windows(self, size: usize) -> Windows<Self>
    where
        Self: Sized,
    {
        Windows::new(self, size)
    }

    /// Turns this iterator into a lending iterator over mutable windows of elements (&mut [Item]).
    ///
    /// `WindowsMut` is backed by a buffer that grows to at most size * 2.
    /// This was chosen as a compromise between memory usage and time complexity:
    /// if the buffer was limited to size `size`, we would need to shift all the elements
    /// on every iteration.
    fn windows_mut(self, size: usize) -> WindowsMut<Self>
    where
        Self: Sized,
    {
        WindowsMut::new(self, size)
    }
}

impl<I: Iterator> ToLendingIterator for I {}