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
#![doc = include_str!("../README.md")]

mod lending_iter;
mod lending_iter_mut;
mod popping_iter;
mod trivial_last_entry;

pub use lending_iter::LendingIter;
pub use lending_iter_mut::LendingIterMut;
pub use popping_iter::PoppingIter;
pub use trivial_last_entry::TrivialLastEntry;

/// An [extension trait] for `Vec<Vec<T>>`.
///
/// [extension trait]: https://doc.rust-lang.org/book/ch10-02-traits.html#extending-a-trait
pub trait VecVecExt {
    /// The type `T` of the items contained in the `Vec<Vec<T>>`.
    type Item;

    /// Returns a [`PoppingIter`] over the `Vec<Vec<T>>`.
    fn popping_iter(&mut self) -> PoppingIter<'_, Self::Item>;

    /// Returns a [lending iterator] over the mutable references to the elements of `Vec<Vec<T>>`.
    ///
    /// [lending iterator]: https://blog.rust-lang.org/2022/10/28/gats-stabilization.html#what-are-gats
    fn lending_iter_mut(&mut self) -> LendingIterMut<'_, Self::Item>;

    /// Returns a [lending iterator] over the shared references to the elements of `Vec<Vec<T>>`.
    fn lending_iter(&self) -> LendingIter<'_, Self::Item>;

    fn trivial_last_entry(&mut self) -> Option<TrivialLastEntry<'_, Self::Item>>;
}

impl<T> VecVecExt for Vec<Vec<T>> {
    type Item = T;

    fn popping_iter(&mut self) -> PoppingIter<'_, Self::Item> {
        PoppingIter(self)
    }

    fn lending_iter_mut(&mut self) -> LendingIterMut<'_, Self::Item> {
        LendingIterMut::new(self)
    }

    fn lending_iter(&self) -> LendingIter<'_, Self::Item> {
        LendingIter::new(self)
    }

    fn trivial_last_entry(&mut self) -> Option<TrivialLastEntry<'_, Self::Item>> {
        TrivialLastEntry::new(self)
    }
}