From 0c1ce664ea127ce905cdd840b6fe1f6ce6758d35 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Thu, 16 Sep 2021 08:19:17 -0700 Subject: [PATCH] store: remove (weak) self-reference and take `&Arc` arguments instead The `weak_self` stuff was from before I knew that `self` could be of type `&Arc`. --- lib/src/index_store.rs | 6 +++--- lib/src/lib.rs | 1 - lib/src/repo.rs | 2 +- lib/src/store.rs | 48 ++++++++++++------------------------------ lib/src/tree.rs | 4 ++-- 5 files changed, 19 insertions(+), 42 deletions(-) diff --git a/lib/src/index_store.rs b/lib/src/index_store.rs index e881f5bb0..e4e038f7d 100644 --- a/lib/src/index_store.rs +++ b/lib/src/index_store.rs @@ -50,7 +50,7 @@ impl IndexStore { IndexStore { dir } } - pub fn get_index_at_op(&self, op: &Operation, store: &Store) -> Arc { + pub fn get_index_at_op(&self, op: &Operation, store: &Arc) -> Arc { let op_id_hex = op.id().hex(); let op_id_file = self.dir.join("operations").join(&op_id_hex); if op_id_file.exists() { @@ -89,7 +89,7 @@ impl IndexStore { fn index_at_operation( &self, - store: &Store, + store: &Arc, operation: &Operation, ) -> io::Result> { let view = operation.view(); @@ -163,7 +163,7 @@ impl IndexStore { // Returns the ancestors of heads with parents and predecessors come before the // commit itself fn topo_order_earlier_first( - store: &Store, + store: &Arc, heads: Vec, parent_file: Option>, ) -> Vec { diff --git a/lib/src/lib.rs b/lib/src/lib.rs index de81cf01c..6a641e736 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -13,7 +13,6 @@ // limitations under the License. #![feature(assert_matches)] -#![feature(get_mut_unchecked)] #![feature(map_first_last)] #![deny(unused_must_use)] diff --git a/lib/src/repo.rs b/lib/src/repo.rs index 131068fa8..a62d43fdb 100644 --- a/lib/src/repo.rs +++ b/lib/src/repo.rs @@ -324,7 +324,7 @@ impl ReadonlyRepo { if locked_index.is_none() { locked_index.replace( self.index_store - .get_index_at_op(&self.operation, self.store.as_ref()), + .get_index_at_op(&self.operation, &self.store), ); } let index: &Arc = locked_index.as_ref().unwrap(); diff --git a/lib/src/store.rs b/lib/src/store.rs index 63b3d51da..f3ec88873 100644 --- a/lib/src/store.rs +++ b/lib/src/store.rs @@ -16,7 +16,7 @@ use std::collections::HashMap; use std::fs::File; use std::io::Read; use std::path::{Path, PathBuf}; -use std::sync::{Arc, RwLock, Weak}; +use std::sync::{Arc, RwLock}; use crate::backend; use crate::backend::{ @@ -34,7 +34,6 @@ use crate::tree_builder::TreeBuilder; /// adds the root commit and adds caching. #[derive(Debug)] pub struct Store { - weak_self: Option>, backend: Box, root_commit_id: CommitId, commit_cache: RwLock>>, @@ -44,17 +43,12 @@ pub struct Store { impl Store { pub fn new(backend: Box) -> Arc { let root_commit_id = CommitId(vec![0; backend.hash_length()]); - let mut wrapper = Arc::new(Store { - weak_self: None, + Arc::new(Store { backend, root_commit_id, commit_cache: Default::default(), tree_cache: Default::default(), - }); - let weak_self = Arc::downgrade(&wrapper); - let mut ref_mut = unsafe { Arc::get_mut_unchecked(&mut wrapper) }; - ref_mut.weak_self = Some(weak_self); - wrapper + }) } pub fn load_store(repo_path: &Path) -> Arc { @@ -96,17 +90,13 @@ impl Store { &self.root_commit_id } - pub fn root_commit(&self) -> Commit { + pub fn root_commit(self: &Arc) -> Commit { self.get_commit(&self.root_commit_id).unwrap() } - pub fn get_commit(&self, id: &CommitId) -> BackendResult { + pub fn get_commit(self: &Arc, id: &CommitId) -> BackendResult { let data = self.get_backend_commit(id)?; - Ok(Commit::new( - self.weak_self.as_ref().unwrap().upgrade().unwrap(), - id.clone(), - data, - )) + Ok(Commit::new(self.clone(), id.clone(), data)) } fn make_root_commit(&self) -> backend::Commit { @@ -151,29 +141,20 @@ impl Store { Ok(data) } - pub fn write_commit(&self, commit: backend::Commit) -> Commit { + pub fn write_commit(self: &Arc, commit: backend::Commit) -> Commit { let commit_id = self.backend.write_commit(&commit).unwrap(); let data = Arc::new(commit); { let mut write_locked_cache = self.commit_cache.write().unwrap(); write_locked_cache.insert(commit_id.clone(), data.clone()); } - let commit = Commit::new( - self.weak_self.as_ref().unwrap().upgrade().unwrap(), - commit_id, - data, - ); - commit + + Commit::new(self.clone(), commit_id, data) } - pub fn get_tree(&self, dir: &RepoPath, id: &TreeId) -> BackendResult { + pub fn get_tree(self: &Arc, dir: &RepoPath, id: &TreeId) -> BackendResult { let data = self.get_backend_tree(dir, id)?; - Ok(Tree::new( - self.weak_self.as_ref().unwrap().upgrade().unwrap(), - dir.clone(), - id.clone(), - data, - )) + Ok(Tree::new(self.clone(), dir.clone(), id.clone(), data)) } fn get_backend_tree(&self, dir: &RepoPath, id: &TreeId) -> BackendResult> { @@ -219,10 +200,7 @@ impl Store { self.backend.write_conflict(contents) } - pub fn tree_builder(&self, base_tree_id: TreeId) -> TreeBuilder { - TreeBuilder::new( - self.weak_self.as_ref().unwrap().upgrade().unwrap(), - base_tree_id, - ) + pub fn tree_builder(self: &Arc, base_tree_id: TreeId) -> TreeBuilder { + TreeBuilder::new(self.clone(), base_tree_id) } } diff --git a/lib/src/tree.rs b/lib/src/tree.rs index dcfd53873..f70fc2fc0 100644 --- a/lib/src/tree.rs +++ b/lib/src/tree.rs @@ -492,7 +492,7 @@ pub fn merge_trees( base_tree: &Tree, side2_tree: &Tree, ) -> Result { - let store = base_tree.store().as_ref(); + let store = base_tree.store(); let dir = base_tree.dir(); assert_eq!(side1_tree.dir(), dir); assert_eq!(side2_tree.dir(), dir); @@ -534,7 +534,7 @@ pub fn merge_trees( } fn merge_tree_value( - store: &Store, + store: &Arc, dir: &RepoPath, basename: &RepoPathComponent, maybe_base: Option<&TreeValue>,