File size: 6,163 Bytes
1e92f2d | 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | use std::future::Future;
use anyhow::Result;
use futures::{StreamExt, stream::FuturesUnordered};
use rustc_hash::FxHashSet;
use super::{
SkipDuplicates, Visit, VisitControlFlow,
graph_store::{GraphNode, GraphStore, SkipDuplicatesWithKey},
with_future::With,
};
/// A list of modules that were already visited and should be skipped (including their subgraphs).
#[derive(Clone, Default, Debug)]
pub struct VisitedNodes<T>(pub FxHashSet<T>);
/// [`GraphTraversal`] is a utility type that can be used to traverse a graph of
/// nodes, where each node can have a variable number of outgoing edges.
///
/// The traversal is done in parallel, and the order of the nodes in the traversal
/// result is determined by the [`GraphStore`] parameter.
pub trait GraphTraversal: GraphStore + Sized {
fn visit<VisitImpl, Abort, Impl>(
self,
root_edges: impl IntoIterator<Item = VisitImpl::Edge>,
visit: VisitImpl,
) -> impl Future<Output = GraphTraversalResult<Result<Self>, Abort>> + Send
where
VisitImpl: Visit<Self::Node, Abort, Impl> + Send,
Abort: Send,
Impl: Send;
fn skip_duplicates(self) -> SkipDuplicates<Self>;
fn skip_duplicates_with_visited_nodes(
self,
visited: VisitedNodes<Self::Node>,
) -> SkipDuplicates<Self>;
fn skip_duplicates_with_key<
Key: Send + Eq + std::hash::Hash + Clone,
KeyExtractor: Send + Fn(&Self::Node) -> &Key,
>(
self,
key_extractor: KeyExtractor,
) -> SkipDuplicatesWithKey<Self, Key, KeyExtractor>;
}
impl<Store> GraphTraversal for Store
where
Store: GraphStore,
{
/// Visits the graph starting from the given `roots`, and returns a future
/// that will resolve to the traversal result.
fn visit<VisitImpl, Abort, Impl>(
mut self,
root_edges: impl IntoIterator<Item = VisitImpl::Edge>,
mut visit: VisitImpl,
) -> impl Future<Output = GraphTraversalResult<Result<Self>, Abort>> + Send
where
VisitImpl: Visit<Self::Node, Abort, Impl> + Send,
Abort: Send,
Impl: Send,
{
let mut futures = FuturesUnordered::new();
let mut root_abort = None;
// Populate `futures` with all the roots, `root_edges` isn't required to be `Send`, so this
// has to happen outside of the future. We could require `root_edges` to be `Send` in the
// future.
for edge in root_edges {
match visit.visit(edge) {
VisitControlFlow::Continue(node) => {
if let Some((parent_handle, node_ref)) = self.insert(None, GraphNode(node)) {
let span = visit.span(node_ref);
futures.push(With::new(visit.edges(node_ref), span, parent_handle));
}
}
VisitControlFlow::Skip(node) => {
self.insert(None, GraphNode(node));
}
VisitControlFlow::Abort(abort) => {
// this must be returned inside the `async` block below so that it's part of the
// returned future
root_abort = Some(abort)
}
}
}
async move {
if let Some(abort) = root_abort {
return GraphTraversalResult::Aborted(abort);
}
loop {
match futures.next().await {
Some((parent_handle, span, Ok(edges))) => {
let _guard = span.enter();
for edge in edges {
match visit.visit(edge) {
VisitControlFlow::Continue(node) => {
if let Some((node_handle, node_ref)) =
self.insert(Some(parent_handle.clone()), GraphNode(node))
{
let span = visit.span(node_ref);
futures.push(With::new(
visit.edges(node_ref),
span,
node_handle,
));
}
}
VisitControlFlow::Skip(node) => {
self.insert(Some(parent_handle.clone()), GraphNode(node));
}
VisitControlFlow::Abort(abort) => {
return GraphTraversalResult::Aborted(abort);
}
}
}
}
Some((_, _, Err(err))) => {
return GraphTraversalResult::Completed(Err(err));
}
None => {
return GraphTraversalResult::Completed(Ok(self));
}
}
}
}
}
fn skip_duplicates(self) -> SkipDuplicates<Self> {
SkipDuplicates::new(self)
}
fn skip_duplicates_with_visited_nodes(
self,
visited: VisitedNodes<Store::Node>,
) -> SkipDuplicates<Self> {
SkipDuplicates::new_with_visited_nodes(self, visited.0)
}
fn skip_duplicates_with_key<
Key: Send + Eq + std::hash::Hash + Clone,
KeyExtractor: Send + Fn(&Self::Node) -> &Key,
>(
self,
key_extractor: KeyExtractor,
) -> SkipDuplicatesWithKey<Self, Key, KeyExtractor> {
SkipDuplicatesWithKey::new(self, key_extractor)
}
}
pub enum GraphTraversalResult<Completed, Aborted> {
Completed(Completed),
Aborted(Aborted),
}
impl<Completed> GraphTraversalResult<Completed, !> {
pub fn completed(self) -> Completed {
match self {
GraphTraversalResult::Completed(completed) => completed,
GraphTraversalResult::Aborted(_) => unreachable!("the type parameter `Aborted` is `!`"),
}
}
}
|