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
use crate::{IntoLending, LendRefs, LendRefsMut, Windows, WindowsMut};
/// An extension trait for iterators that allows turning them into lending iterators (over windows of elements).
pub trait ToLendingIterator: IntoIterator {
/// 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::IntoIter>
where
Self: Sized,
{
Windows::new(self.into_iter(), 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::IntoIter>
where
Self: Sized,
{
WindowsMut::new(self.into_iter(), size)
}
/// Turns this iterator into a lending iterator trivially.
fn into_lending(self) -> IntoLending<Self::IntoIter>
where
Self: Sized,
{
IntoLending::new(self.into_iter())
}
/// Turns this iterator into a lending iterator that lends references
/// to the iterator's items.
fn lend_refs(self) -> LendRefs<Self::IntoIter>
where
Self: Sized,
{
LendRefs::new(self.into_iter())
}
/// Turns this iterator into a lending iterator that lends mutable references
/// to the iterator's items.
fn lend_refs_mut(self) -> LendRefsMut<Self::IntoIter>
where
Self: Sized,
{
LendRefsMut::new(self.into_iter())
}
}
impl<I: IntoIterator> ToLendingIterator for I {}