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<'_>.
This commit is contained in:
Yuya Nishihara 2023-05-26 16:01:59 +09:00
parent b9c8fd8ef3
commit 5989bdf781
4 changed files with 11 additions and 11 deletions

View file

@ -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<Self>) -> Box<dyn Any> {
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)
}

View file

@ -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<CommitId>;
@ -79,6 +77,8 @@ pub trait ReadonlyIndex: Send + Sync {
}
pub trait MutableIndex: Any {
fn as_any(&self) -> &dyn Any;
fn into_any(self: Box<Self>) -> Box<dyn Any>;
fn as_index(&self) -> &dyn Index;

View file

@ -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()

View file

@ -457,7 +457,7 @@ fn as_readonly_composite(repo: &Arc<ReadonlyRepo>) -> CompositeIndex<'_> {
}
fn as_mutable_impl(repo: &MutableRepo) -> &MutableIndexImpl {
repo.index()
repo.mutable_index()
.as_any()
.downcast_ref::<MutableIndexImpl>()
.unwrap()