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 over topological traversal.
27
28use super::Traversal;
29
30// ----------------------------------------------------------------------------
31// Structs
32// ----------------------------------------------------------------------------
33
34/// Consuming iterator over topological traversal.
35///
36/// This iterator consumes a [`Traversal`], emitting nodes in topological order.
37/// It offers a simplified API for synchronous iteration if nodes don't need to
38/// be deliberately completed, but can be considered done once the iterator
39/// has emitted them.
40#[derive(Debug)]
41pub struct IntoIter {
42    /// Traversal.
43    traversal: Traversal,
44}
45
46// ----------------------------------------------------------------------------
47// Trait implementations
48// ----------------------------------------------------------------------------
49
50impl IntoIterator for Traversal {
51    type Item = usize;
52    type IntoIter = IntoIter;
53
54    /// Creates a consuming iterator over the topological traversal.
55    ///
56    /// This consumes the traversal and produces an iterator that automatically
57    /// completes each node after emitting it, allowing for convenient use in
58    /// for loops and iterator chains.
59    ///
60    /// # Examples
61    ///
62    /// ```
63    /// # use std::error::Error;
64    /// # fn main() -> Result<(), Box<dyn Error>> {
65    /// use zrx_graph::Graph;
66    ///
67    /// // Create graph builder and add nodes
68    /// let mut builder = Graph::builder();
69    /// let a = builder.add_node("a");
70    /// let b = builder.add_node("b");
71    /// let c = builder.add_node("c");
72    ///
73    /// // Create edges between nodes
74    /// builder.add_edge(a, b)?;
75    /// builder.add_edge(b, c)?;
76    ///
77    /// // Create graph from builder
78    /// let graph = builder.build();
79    ///
80    /// // Create iterator over topological traversal
81    /// for node in graph.traverse([a]) {
82    ///     println!("{node:?}");
83    /// }
84    /// # Ok(())
85    /// # }
86    /// ```
87    #[inline]
88    fn into_iter(self) -> Self::IntoIter {
89        IntoIter { traversal: self }
90    }
91}
92
93// ----------------------------------------------------------------------------
94
95impl Iterator for IntoIter {
96    type Item = usize;
97
98    /// Returns the next node.
99    #[inline]
100    fn next(&mut self) -> Option<Self::Item> {
101        let node = self.traversal.take()?;
102        self.traversal.complete(node).expect("invariant");
103        Some(node)
104    }
105
106    /// Returns the bounds on the remaining length of the traversal.
107    #[inline]
108    fn size_hint(&self) -> (usize, Option<usize>) {
109        (self.traversal.len(), None)
110    }
111}