Skip to main content

zrx_graph/graph/traversal/
into_iter.rs

1// Copyright (c) 2025-2026 Zensical and contributors
2
3// SPDX-License-Identifier: MIT
4// All contributions are certified under the DCO
5
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to
8// deal in the Software without restriction, including without limitation the
9// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10// sell copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12
13// The above copyright notice and this permission notice shall be included in
14// all copies or substantial portions of the Software.
15
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22// IN THE SOFTWARE.
23
24// ----------------------------------------------------------------------------
25
26//! Consuming iterator implementation for [`Traversal`].
27
28use super::Traversal;
29
30// ----------------------------------------------------------------------------
31// Structs
32// ----------------------------------------------------------------------------
33
34/// Consuming iterator for [`Traversal`].
35#[derive(Debug)]
36pub struct IntoIter {
37    /// Traversal.
38    traversal: Traversal,
39}
40
41// ----------------------------------------------------------------------------
42// Trait implementations
43// ----------------------------------------------------------------------------
44
45impl IntoIterator for Traversal {
46    type Item = usize;
47    type IntoIter = IntoIter;
48
49    /// Creates a consuming iterator over the topological traversal.
50    ///
51    /// This consumes the traversal and produces an iterator that automatically
52    /// completes each node after emitting it, allowing for convenient use in
53    /// for loops and iterator chains.
54    ///
55    /// # Examples
56    ///
57    /// ```
58    /// # use std::error::Error;
59    /// # fn main() -> Result<(), Box<dyn Error>> {
60    /// use zrx_graph::Graph;
61    ///
62    /// // Create graph builder and add nodes
63    /// let mut builder = Graph::builder();
64    /// let a = builder.add_node("a");
65    /// let b = builder.add_node("b");
66    /// let c = builder.add_node("c");
67    ///
68    /// // Create edges between nodes
69    /// builder.add_edge(a, b)?;
70    /// builder.add_edge(b, c)?;
71    ///
72    /// // Create graph from builder
73    /// let graph = builder.build();
74    ///
75    /// // Create topological traversal
76    /// for node in graph.traverse([a]) {
77    ///     println!("{node:?}");
78    /// }
79    /// # Ok(())
80    /// # }
81    /// ```
82    #[inline]
83    fn into_iter(self) -> Self::IntoIter {
84        IntoIter { traversal: self }
85    }
86}
87
88// ----------------------------------------------------------------------------
89
90impl Iterator for IntoIter {
91    type Item = usize;
92
93    /// Returns the next node.
94    #[inline]
95    fn next(&mut self) -> Option<Self::Item> {
96        let node = self.traversal.take()?;
97        self.traversal.complete(node).expect("invariant");
98        Some(node)
99    }
100
101    /// Returns the bounds on the remaining length of the traversal.
102    #[inline]
103    fn size_hint(&self) -> (usize, Option<usize>) {
104        (self.traversal.len(), None)
105    }
106}