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> where T: 'a, V: 'a {
15  spline: &'a Spline<T, V>,
16  i: usize
17}
18
19impl<'a, T, V> Iterator for Iter<'a, T, V> {
20  type Item = &'a Key<T, V>;
21
22  fn next(&mut self) -> Option<Self::Item> {
23    let r = self.spline.0.get(self.i);
24
25    if let Some(_) = r {
26      self.i += 1;
27    }
28
29    r
30  }
31}
32
33impl<'a, T, V> IntoIterator for &'a Spline<T, V> {
34  type Item = &'a Key<T, V>;
35  type IntoIter = Iter<'a, T, V>;
36
37  fn into_iter(self) -> Self::IntoIter {
38    Iter {
39      spline: self,
40      i: 0
41    }
42  }
43}
44