changeset 46058:12192fdbf3ac

copies-rust: move the parent token to an enum We carry around information about which parent of a revision is been dealt with. So far this was a `usize` but as we are about to pass it around to more function it seems like a good idea to start cleaning this up and use a proper enum. Differential Revision: https://phab.mercurial-scm.org/D9420
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Fri, 20 Nov 2020 14:03:40 +0100
parents e0313b0a6f7e
children dacb771f6dd2
files rust/hg-core/src/copy_tracing.rs
diffstat 1 files changed, 20 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/rust/hg-core/src/copy_tracing.rs	Thu Nov 12 15:54:10 2020 +0100
+++ b/rust/hg-core/src/copy_tracing.rs	Fri Nov 20 14:03:40 2020 +0100
@@ -186,7 +186,7 @@
     }
 
     /// Return an iterator over all the `Action` in this instance.
-    fn iter_actions(&self, parent: usize) -> ActionsIterator {
+    fn iter_actions(&self, parent: Parent) -> ActionsIterator {
         ActionsIterator {
             changes: &self,
             parent: parent,
@@ -259,7 +259,7 @@
 
 struct ActionsIterator<'a> {
     changes: &'a ChangedFiles<'a>,
-    parent: usize,
+    parent: Parent,
     current: u32,
 }
 
@@ -267,6 +267,10 @@
     type Item = Action<'a>;
 
     fn next(&mut self) -> Option<Action<'a>> {
+        let copy_flag = match self.parent {
+            Parent::FirstParent => P1_COPY,
+            Parent::SecondParent => P2_COPY,
+        };
         while self.current < self.changes.nb_items {
             let (flags, file, source) = self.changes.entry(self.current);
             self.current += 1;
@@ -274,10 +278,7 @@
                 return Some(Action::Removed(file));
             }
             let copy = flags & COPY_MASK;
-            if self.parent == 1 && copy == P1_COPY {
-                return Some(Action::Copied(file, source));
-            }
-            if self.parent == 2 && copy == P2_COPY {
+            if copy == copy_flag {
                 return Some(Action::Copied(file, source));
             }
         }
@@ -301,6 +302,15 @@
 pub type RevInfoMaker<'a, D> =
     Box<dyn for<'r> Fn(Revision, &'r mut DataHolder<D>) -> RevInfo<'r> + 'a>;
 
+/// enum used to carry information about the parent → child currently processed
+#[derive(Copy, Clone, Debug)]
+enum Parent {
+    /// The `p1(x) → x` edge
+    FirstParent,
+    /// The `p2(x) → x` edge
+    SecondParent,
+}
+
 /// Same as mercurial.copies._combine_changeset_copies, but in Rust.
 ///
 /// Arguments are:
@@ -345,10 +355,10 @@
             let (p1, p2, changes) = rev_info(*child, &mut d);
 
             let parent = if rev == p1 {
-                1
+                Parent::FirstParent
             } else {
                 assert_eq!(rev, p2);
-                2
+                Parent::SecondParent
             };
             let mut new_copies = copies.clone();
 
@@ -406,9 +416,8 @@
                 }
                 Some(other_copies) => {
                     let (minor, major) = match parent {
-                        1 => (other_copies, new_copies),
-                        2 => (new_copies, other_copies),
-                        _ => unreachable!(),
+                        Parent::FirstParent => (other_copies, new_copies),
+                        Parent::SecondParent => (new_copies, other_copies),
                     };
                     let merged_copies =
                         merge_copies_dict(minor, major, &changes, &mut oracle);