trie_rs/iter/keys.rs
1#[derive(Debug, Clone)]
2/// Retains keys and strips off `Value`s from a [crate::iter] iterator.
3pub struct Keys<I>(I);
4
5impl<I> Keys<I> {
6 ///Creates a new `Keys` iterator.
7 pub fn new(iter: I) -> Self {
8 Self(iter)
9 }
10}
11
12// TODO: This is generic for V, which is a stand-in for the Value, but in a
13// `map::Trie<K,V>`, its iterators will actually reurn `(C, &V)`. Hopefully that
14// won't matter.
15impl<I, C, V> Iterator for Keys<I>
16where
17 I: Iterator<Item = (C, V)>,
18{
19 type Item = C;
20 fn next(&mut self) -> Option<C> {
21 self.0.next().map(|x| x.0)
22 }
23}
24
25/// Strip an iterator items `(K, V)` to only have `K`.
26pub trait KeysExt: Iterator {
27 /// Retain keys and strip values from a [crate::iter] iterator.
28 fn keys(self) -> Keys<Self>
29 where
30 Self: Sized,
31 {
32 Keys::new(self)
33 }
34}
35
36impl<T> KeysExt for T where T: Iterator + ?Sized {}