Patchwork [09,of,11] rust-dirstate: remove excessive clone() of parameter and return value

login
register
mail settings
Submitter Yuya Nishihara
Date Aug. 17, 2019, 12:12 p.m.
Message ID <d4c0130723d92bd545e7.1566043930@mimosa>
Download mbox | patch
Permalink /patch/41331/
State Accepted
Headers show

Comments

Yuya Nishihara - Aug. 17, 2019, 12:12 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1566027185 -32400
#      Sat Aug 17 16:33:05 2019 +0900
# Node ID d4c0130723d92bd545e7df45aceead8bd7562156
# Parent  5f76ca22fb8fb29ec0a60ec0af88cb328f7a0c9e
rust-dirstate: remove excessive clone() of parameter and return value

I think pass-by-ref is preferred in general.

Patch

diff --git a/rust/hg-core/src/dirstate/dirstate_map.rs b/rust/hg-core/src/dirstate/dirstate_map.rs
--- a/rust/hg-core/src/dirstate/dirstate_map.rs
+++ b/rust/hg-core/src/dirstate/dirstate_map.rs
@@ -68,7 +68,7 @@  impl DirstateMap {
         self.file_fold_map = None;
         self.non_normal_set.clear();
         self.other_parent_set.clear();
-        self.set_parents(DirstateParents {
+        self.set_parents(&DirstateParents {
             p1: NULL_ID,
             p2: NULL_ID,
         })
@@ -253,9 +253,9 @@  impl DirstateMap {
     pub fn parents(
         &mut self,
         file_contents: &[u8],
-    ) -> Result<DirstateParents, DirstateError> {
+    ) -> Result<&DirstateParents, DirstateError> {
         if let Some(ref parents) = self.parents {
-            return Ok(parents.clone());
+            return Ok(parents);
         }
         let parents;
         if file_contents.len() == PARENT_SIZE * 2 {
@@ -274,11 +274,11 @@  impl DirstateMap {
             return Err(DirstateError::Parse(DirstateParseError::Damaged));
         }
 
-        self.parents = Some(parents.to_owned());
-        Ok(parents.clone())
+        self.parents = Some(parents);
+        Ok(self.parents.as_ref().unwrap())
     }
 
-    pub fn set_parents(&mut self, parents: DirstateParents) {
+    pub fn set_parents(&mut self, parents: &DirstateParents) {
         self.parents = Some(parents.clone());
         self.dirty_parents = true;
     }
@@ -298,7 +298,7 @@  impl DirstateMap {
         )?;
 
         if !self.dirty_parents {
-            self.set_parents(parents.to_owned());
+            self.set_parents(&parents);
         }
 
         Ok(Some(parents))
@@ -320,9 +320,9 @@  impl DirstateMap {
         Ok(packed)
     }
 
-    pub fn build_file_fold_map(&mut self) -> FileFoldMap {
+    pub fn build_file_fold_map(&mut self) -> &FileFoldMap {
         if let Some(ref file_fold_map) = self.file_fold_map {
-            return file_fold_map.to_owned();
+            return file_fold_map;
         }
         let mut new_file_fold_map = FileFoldMap::new();
         for (filename, DirstateEntry { state, .. }) in self.state_map.borrow()
@@ -335,7 +335,7 @@  impl DirstateMap {
             }
         }
         self.file_fold_map = Some(new_file_fold_map);
-        self.file_fold_map.to_owned().unwrap()
+        self.file_fold_map.as_ref().unwrap()
     }
 }
 
diff --git a/rust/hg-cpython/src/dirstate/dirstate_map.rs b/rust/hg-cpython/src/dirstate/dirstate_map.rs
--- a/rust/hg-cpython/src/dirstate/dirstate_map.rs
+++ b/rust/hg-cpython/src/dirstate/dirstate_map.rs
@@ -243,7 +243,7 @@  py_class!(pub class DirstateMap |py| {
 
         self.inner(py)
             .borrow_mut()
-            .set_parents(DirstateParents { p1, p2 });
+            .set_parents(&DirstateParents { p1, p2 });
         Ok(py.None())
     }