From 5989bdf7810c26d428af01cd77286d874f925bf9 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Fri, 26 May 2023 16:01:59 +0900 Subject: [PATCH] index: move Index::as_any() to MutableIndex, obtain CompositeIndex from there It might sound scary to add public .mutable_index() accessor, but I think it's okay because immutable MutableIndex reference has no more power than Index. This allows us to implement Index for lifetime-bound type such as CompositeIndex<'_>. --- lib/src/default_index_store.rs | 12 ++++-------- lib/src/index.rs | 4 ++-- lib/src/repo.rs | 4 ++++ lib/tests/test_index.rs | 2 +- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/src/default_index_store.rs b/lib/src/default_index_store.rs index d6de37a9c..3e28ad23b 100644 --- a/lib/src/default_index_store.rs +++ b/lib/src/default_index_store.rs @@ -688,10 +688,6 @@ impl MutableIndexImpl { } impl Index for MutableIndexImpl { - fn as_any(&self) -> &dyn Any { - self - } - fn shortest_unique_commit_id_prefix_len(&self, commit_id: &CommitId) -> usize { CompositeIndex(self).shortest_unique_commit_id_prefix_len(commit_id) } @@ -732,6 +728,10 @@ impl Index for MutableIndexImpl { } impl MutableIndex for MutableIndexImpl { + fn as_any(&self) -> &dyn Any { + self + } + fn into_any(self: Box) -> Box { Box::new(*self) } @@ -1976,10 +1976,6 @@ impl ReadonlyIndexImpl { } impl Index for ReadonlyIndexImpl { - fn as_any(&self) -> &dyn Any { - self - } - fn shortest_unique_commit_id_prefix_len(&self, commit_id: &CommitId) -> usize { CompositeIndex(self).shortest_unique_commit_id_prefix_len(commit_id) } diff --git a/lib/src/index.rs b/lib/src/index.rs index 892f5ecf7..0e85432d4 100644 --- a/lib/src/index.rs +++ b/lib/src/index.rs @@ -46,8 +46,6 @@ pub trait IndexStore: Send + Sync + Debug { } pub trait Index: Send + Sync { - fn as_any(&self) -> &dyn Any; - fn shortest_unique_commit_id_prefix_len(&self, commit_id: &CommitId) -> usize; fn resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution; @@ -79,6 +77,8 @@ pub trait ReadonlyIndex: Send + Sync { } pub trait MutableIndex: Any { + fn as_any(&self) -> &dyn Any; + fn into_any(self: Box) -> Box; fn as_index(&self) -> &dyn Index; diff --git a/lib/src/repo.rs b/lib/src/repo.rs index 48e3689aa..1f74ee559 100644 --- a/lib/src/repo.rs +++ b/lib/src/repo.rs @@ -662,6 +662,10 @@ impl MutableRepo { self.view.get_mut() } + pub fn mutable_index(&self) -> &dyn MutableIndex { + self.index.as_ref() + } + pub fn has_changes(&self) -> bool { !(self.abandoned_commits.is_empty() && self.rewritten_commits.is_empty() diff --git a/lib/tests/test_index.rs b/lib/tests/test_index.rs index 56ffc3b9f..95638d530 100644 --- a/lib/tests/test_index.rs +++ b/lib/tests/test_index.rs @@ -457,7 +457,7 @@ fn as_readonly_composite(repo: &Arc) -> CompositeIndex<'_> { } fn as_mutable_impl(repo: &MutableRepo) -> &MutableIndexImpl { - repo.index() + repo.mutable_index() .as_any() .downcast_ref::() .unwrap()