dirstate-tree: simplify the control flow in the Node.insert method
authorPierre-Yves David <pierre-yves.david@octobus.net>
Fri, 09 Oct 2020 10:33:19 +0200
changeset 45780 ae2873e92250
parent 45779 8719a5b68419
child 45781 b1664f6eb650
dirstate-tree: simplify the control flow in the Node.insert method But explicitly with the special case early, laying out the various case become simpler. (The initial motivation was to make some future lifetime error simpler). Differential Revision: https://phab.mercurial-scm.org/D9203
rust/hg-core/src/dirstate/dirstate_tree/node.rs
--- a/rust/hg-core/src/dirstate/dirstate_tree/node.rs	Wed Oct 21 01:48:09 2020 +0200
+++ b/rust/hg-core/src/dirstate/dirstate_tree/node.rs	Fri Oct 09 10:33:19 2020 +0200
@@ -60,43 +60,46 @@
         // Are we're modifying the current file ? Is the the end of the path ?
         let is_current_file = tail.is_empty() && head.is_empty();
 
-        if let NodeKind::File(file) = &mut self.kind {
-            if is_current_file {
-                let new = Self {
-                    kind: NodeKind::File(File {
-                        entry: new_entry,
-                        ..file.clone()
-                    }),
-                };
-                return InsertResult {
-                    did_insert: false,
-                    old_entry: Some(std::mem::replace(self, new)),
-                };
-            } else {
-                match file.entry.state {
-                    // Only replace the current file with a directory if it's
-                    // marked as `Removed`
-                    EntryState::Removed => {
-                        self.kind = NodeKind::Directory(Directory {
-                            was_file: Some(Box::from(file.clone())),
-                            children: Default::default(),
-                        })
-                    }
-                    _ => {
-                        return Node::insert_in_file(
-                            file, new_entry, head, tail,
-                        )
-                    }
+        // Potentially Replace the current file with a directory if it's marked
+        // as `Removed`
+        if !is_current_file {
+            if let NodeKind::File(file) = &mut self.kind {
+                if file.entry.state == EntryState::Removed {
+                    self.kind = NodeKind::Directory(Directory {
+                        was_file: Some(Box::from(file.clone())),
+                        children: Default::default(),
+                    })
                 }
             }
         }
-
         match &mut self.kind {
             NodeKind::Directory(directory) => {
                 Node::insert_in_directory(directory, new_entry, head, tail)
             }
-            NodeKind::File(_) => {
-                unreachable!("The file case has already been handled")
+            NodeKind::File(file) => {
+                if is_current_file {
+                    let new = Self {
+                        kind: NodeKind::File(File {
+                            entry: new_entry,
+                            ..file.clone()
+                        }),
+                    };
+                    InsertResult {
+                        did_insert: false,
+                        old_entry: Some(std::mem::replace(self, new)),
+                    }
+                } else {
+                    match file.entry.state {
+                        EntryState::Removed => {
+                            unreachable!("Removed file turning into a directory was dealt with earlier")
+                        }
+                        _ => {
+                            Node::insert_in_file(
+                                file, new_entry, head, tail,
+                            )
+                        }
+                    }
+                }
             }
         }
     }