generalize the path_to_top from the links errors
authorEh2406 <YeomanYaacov@gmail.com>
Wed, 7 Feb 2018 22:50:03 +0000 (17:50 -0500)
committerEh2406 <YeomanYaacov@gmail.com>
Thu, 8 Feb 2018 19:13:07 +0000 (14:13 -0500)
src/cargo/core/resolver/mod.rs
src/cargo/util/graph.rs

index 173dcf7cae56d63964282956f0263be47d4b0ba2..fe23ce442292055a51fe9653b466d85ee52bcdf8 100644 (file)
@@ -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>>) {
index d97b9d44d445840d963bd16a07efd426d21a6691..d55e410cd3ec9bcb6d487d78999bdd343c67fc2a 100644 (file)
@@ -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> {