changeset 40933:18513d6ef7d4

rust: changed Graph.parents to return [Revision; 2] This will allow for simple iteration on parent revisions, such as: for parent in graph.parents(rev)?.iter().cloned() This seems to be a zero overhead abstraction once built in release mode. Differential Revision: https://phab.mercurial-scm.org/D5415
author Georges Racinet <gracinet@anybox.fr>
date Fri, 30 Nov 2018 00:44:04 +0100
parents dc38d976ff4d
children d3e688b9ef2e
files rust/hg-core/src/ancestors.rs rust/hg-core/src/lib.rs rust/hg-direct-ffi/src/ancestors.rs
diffstat 3 files changed, 26 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/rust/hg-core/src/ancestors.rs	Tue Dec 11 17:31:54 2018 +0100
+++ b/rust/hg-core/src/ancestors.rs	Fri Nov 30 00:44:04 2018 +0100
@@ -57,9 +57,9 @@
         };
         this.seen.insert(NULL_REVISION);
         for rev in filtered_initrevs {
-            let parents = this.graph.parents(rev)?;
-            this.conditionally_push_rev(parents.0);
-            this.conditionally_push_rev(parents.1);
+            for parent in this.graph.parents(rev)?.iter().cloned() {
+                this.conditionally_push_rev(parent);
+            }
         }
         Ok(this)
     }
@@ -115,7 +115,7 @@
             }
             Some(c) => *c,
         };
-        let (p1, p2) = match self.graph.parents(current) {
+        let [p1, p2] = match self.graph.parents(current) {
             Ok(ps) => ps,
             Err(e) => return Some(Err(e)),
         };
@@ -141,25 +141,22 @@
 
     /// This is the same as the dict from test-ancestors.py
     impl Graph for Stub {
-        fn parents(
-            &self,
-            rev: Revision,
-        ) -> Result<(Revision, Revision), GraphError> {
+        fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> {
             match rev {
-                0 => Ok((-1, -1)),
-                1 => Ok((0, -1)),
-                2 => Ok((1, -1)),
-                3 => Ok((1, -1)),
-                4 => Ok((2, -1)),
-                5 => Ok((4, -1)),
-                6 => Ok((4, -1)),
-                7 => Ok((4, -1)),
-                8 => Ok((-1, -1)),
-                9 => Ok((6, 7)),
-                10 => Ok((5, -1)),
-                11 => Ok((3, 7)),
-                12 => Ok((9, -1)),
-                13 => Ok((8, -1)),
+                0 => Ok([-1, -1]),
+                1 => Ok([0, -1]),
+                2 => Ok([1, -1]),
+                3 => Ok([1, -1]),
+                4 => Ok([2, -1]),
+                5 => Ok([4, -1]),
+                6 => Ok([4, -1]),
+                7 => Ok([4, -1]),
+                8 => Ok([-1, -1]),
+                9 => Ok([6, 7]),
+                10 => Ok([5, -1]),
+                11 => Ok([3, 7]),
+                12 => Ok([9, -1]),
+                13 => Ok([8, -1]),
                 r => Err(GraphError::ParentOutOfRange(r)),
             }
         }
@@ -231,12 +228,9 @@
     struct Corrupted;
 
     impl Graph for Corrupted {
-        fn parents(
-            &self,
-            rev: Revision,
-        ) -> Result<(Revision, Revision), GraphError> {
+        fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> {
             match rev {
-                1 => Ok((0, -1)),
+                1 => Ok([0, -1]),
                 r => Err(GraphError::ParentOutOfRange(r)),
             }
         }
--- a/rust/hg-core/src/lib.rs	Tue Dec 11 17:31:54 2018 +0100
+++ b/rust/hg-core/src/lib.rs	Fri Nov 30 00:44:04 2018 +0100
@@ -15,7 +15,7 @@
 
 /// The simplest expression of what we need of Mercurial DAGs.
 pub trait Graph {
-    fn parents(&self, Revision) -> Result<(Revision, Revision), GraphError>;
+    fn parents(&self, Revision) -> Result<[Revision; 2], GraphError>;
 }
 
 #[derive(Clone, Debug, PartialEq)]
--- a/rust/hg-direct-ffi/src/ancestors.rs	Tue Dec 11 17:31:54 2018 +0100
+++ b/rust/hg-direct-ffi/src/ancestors.rs	Fri Nov 30 00:44:04 2018 +0100
@@ -44,12 +44,12 @@
 
 impl Graph for Index {
     /// wrap a call to the C extern parents function
-    fn parents(&self, rev: Revision) -> Result<(Revision, Revision), GraphError> {
+    fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> {
         let mut res: [c_int; 2] = [0; 2];
         let code =
             unsafe { HgRevlogIndex_GetParents(self.index, rev, &mut res as *mut [c_int; 2]) };
         match code {
-            0 => Ok((res[0], res[1])),
+            0 => Ok(res),
             _ => Err(GraphError::ParentOutOfRange(rev)),
         }
     }
@@ -176,10 +176,10 @@
     struct Stub;
 
     impl Graph for Stub {
-        fn parents(&self, r: Revision) -> Result<(Revision, Revision), GraphError> {
+        fn parents(&self, r: Revision) -> Result<[Revision; 2], GraphError> {
             match r {
                 25 => Err(GraphError::ParentOutOfRange(25)),
-                _ => Ok((1, 2)),
+                _ => Ok([1, 2]),
             }
         }
     }