From: Eh2406 Date: Wed, 7 Feb 2018 22:50:03 +0000 (-0500) Subject: generalize the path_to_top from the links errors X-Git-Tag: archive/raspbian/0.35.0-2+rpi1~3^2^2^2^2^2^2^2~22^2~3^2~6^2~1 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=5a5b5fce420f30f2a9738f2f8bc2ebac74daedd3;p=cargo.git generalize the path_to_top from the links errors --- diff --git a/src/cargo/core/resolver/mod.rs b/src/cargo/core/resolver/mod.rs index 173dcf7ca..fe23ce442 100644 --- a/src/cargo/core/resolver/mod.rs +++ b/src/cargo/core/resolver/mod.rs @@ -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>) { diff --git a/src/cargo/util/graph.rs b/src/cargo/util/graph.rs index d97b9d44d..d55e410cd 100644 --- a/src/cargo/util/graph.rs +++ b/src/cargo/util/graph.rs @@ -67,6 +67,30 @@ impl Graph { pub fn iter(&self) -> Nodes { 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 Default for Graph {