Skip to content

Commit

Permalink
generalize the path_to_top from the links errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Eh2406 committed Feb 8, 2018
1 parent 22ff9fc commit 5a5b5fc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
24 changes: 2 additions & 22 deletions src/cargo/core/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,28 +118,8 @@ struct Candidate {
impl Resolve {
/// Resolves one of the paths from the given dependent package up to
/// the root.
pub fn path_to_top<'a>(&'a self, mut pkg: &'a PackageId) -> Vec<&'a PackageId> {
// Note that this implementation isn't the most robust per se, we'll
// likely have to tweak this over time. For now though it works for what
// it's used for!
let mut result = vec![pkg];
let first_pkg_depending_on = |pkg: &PackageId| {
self.graph.get_nodes()
.iter()
.filter(|&(_node, adjacent)| adjacent.contains(pkg))
.next()
.map(|p| p.0)
};
while let Some(p) = first_pkg_depending_on(pkg) {
// Note that we can have "cycles" introduced through dev-dependency
// edges, so make sure we don't loop infinitely.
if result.contains(&p) {
break
}
result.push(p);
pkg = p;
}
result
pub fn path_to_top<'a>(&'a self, pkg: &'a PackageId) -> Vec<&'a PackageId> {
self.graph.path_to_top(pkg)
}
pub fn register_used_patches(&mut self,
patches: &HashMap<Url, Vec<Summary>>) {
Expand Down
24 changes: 24 additions & 0 deletions src/cargo/util/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,30 @@ impl<N: Eq + Hash + Clone> Graph<N> {
pub fn iter(&self) -> Nodes<N> {
self.nodes.keys()
}

/// Resolves one of the paths from the given dependent package up to
/// the root.
pub fn path_to_top<'a>(&'a self, mut pkg: &'a N) -> Vec<&'a N> {
// Note that this implementation isn't the most robust per se, we'll
// likely have to tweak this over time. For now though it works for what
// it's used for!
let mut result = vec![pkg];
let first_pkg_depending_on = |pkg: &N, res: &[&N]| {
self.get_nodes()
.iter()
.filter(|&(_node, adjacent)| adjacent.contains(pkg))
// Note that we can have "cycles" introduced through dev-dependency
// edges, so make sure we don't loop infinitely.
.filter(|&(_node, _)| !res.contains(&_node))
.next()
.map(|p| p.0)
};
while let Some(p) = first_pkg_depending_on(pkg, &result) {
result.push(p);
pkg = p;
}
result
}
}

impl<N: Eq + Hash + Clone> Default for Graph<N> {
Expand Down

0 comments on commit 5a5b5fc

Please sign in to comment.