splines/
iter.rs

1//! Spline [`Iterator`], in a nutshell.
2//!
3//! You can iterate over a [`Spline<K, V>`]’s keys with the [`IntoIterator`] trait on
4//! `&Spline<K, V>`. This gives you iterated [`Key<K, V>`] keys.
5//!
6//! [`Spline<K, V>`]: crate::spline::Spline
7//! [`Key<K, V>`]: crate::key::Key
8
9use crate::{Key, Spline};
10
11/// Iterator over spline keys.
12///
13/// This iterator type is guaranteed to iterate over sorted keys.
14pub struct Iter<'a, T, V>
15where
16  T: 'a,
17  V: 'a,
18{
19  spline: &'a Spline<T, V>,
20  i: usize,
21}
22
23impl<'a, T, V> Iterator for Iter<'a, T, V> {
24  type Item = &'a Key<T, V>;
25
26  fn next(&mut self) -> Option<Self::Item> {
27    let r = self.spline.0.get(self.i);
28
29    if r.is_some() {
30      self.i += 1;
31    }
32
33    r
34  }
35}
36
37impl<'a, T, V> IntoIterator for &'a Spline<T, V> {
38  type Item = &'a Key<T, V>;
39  type IntoIter = Iter<'a, T, V>;
40
41  fn into_iter(self) -> Self::IntoIter {
42    Self::IntoIter { spline: self, i: 0 }
43  }
44}