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
//! Graph Node related constructs.

use crate::graph::edge::CompactDirection;
use indexmap::map::Keys;
use std::fmt::Debug;
use std::hash::Hash;
use std::iter::Cloned;

/// A trait group for `Graph`'s node identifier.
pub trait NodeTrait: Copy + Debug + Hash + Ord {}

/// Implement the `NodeTrait` for all types satisfying bounds.
impl<N> NodeTrait for N where N: Copy + Debug + Hash + Ord {}

/// Iterator over Nodes.
pub struct Nodes<'a, N: 'a + NodeTrait> {
    iter: Cloned<Keys<'a, N, Vec<(N, CompactDirection)>>>,
}

impl<'a, N: 'a + NodeTrait> Nodes<'a, N> {
    pub fn new(iter: Cloned<Keys<'a, N, Vec<(N, CompactDirection)>>>) -> Self {
        Self { iter }
    }
}

impl<'a, N: 'a + NodeTrait> Iterator for Nodes<'a, N> {
    type Item = N;
    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next()
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.iter.size_hint()
    }
}