diff --git a/compiler/rustc_data_structures/src/graph/mod.rs b/compiler/rustc_data_structures/src/graph/mod.rs index 0906c04716bd0..103ddd917bf84 100644 --- a/compiler/rustc_data_structures/src/graph/mod.rs +++ b/compiler/rustc_data_structures/src/graph/mod.rs @@ -52,3 +52,29 @@ where { iterate::DepthFirstSearch::new(graph).with_start_node(from) } + +pub fn depth_first_search_as_undirected( + graph: G, + from: G::Node, +) -> iterate::DepthFirstSearch> +where + G: Successors + Predecessors, +{ + struct AsUndirected(G); + + impl DirectedGraph for AsUndirected { + type Node = G::Node; + + fn num_nodes(&self) -> usize { + self.0.num_nodes() + } + } + + impl Successors for AsUndirected { + fn successors(&self, node: Self::Node) -> impl Iterator { + self.0.successors(node).chain(self.0.predecessors(node)) + } + } + + iterate::DepthFirstSearch::new(AsUndirected(graph)).with_start_node(from) +}