index: delete IndexRef, use Index trait

I don't know why I didn't create a trait to begin with. Maybe I had
trouble with lifetimes or object-safety.
This commit is contained in:
Martin von Zweigbergk 2023-02-12 21:57:49 -08:00 committed by Martin von Zweigbergk
parent b955e3de03
commit 2d8aa2d90e
5 changed files with 15 additions and 132 deletions

View file

@ -44,7 +44,6 @@ pub struct IndexPosition(u32);
impl IndexPosition {
pub const MAX: Self = IndexPosition(u32::MAX);
}
pub trait Index {
fn num_commits(&self) -> u32;
@ -73,110 +72,6 @@ pub trait Index {
fn topo_order(&self, input: &mut dyn Iterator<Item = &CommitId>) -> Vec<IndexEntry>;
}
#[derive(Clone, Copy)]
pub enum IndexRef<'a> {
Readonly(&'a ReadonlyIndex),
Mutable(&'a MutableIndex),
}
impl<'a> From<&'a ReadonlyIndex> for IndexRef<'a> {
fn from(index: &'a ReadonlyIndex) -> Self {
IndexRef::Readonly(index)
}
}
impl<'a> From<&'a MutableIndex> for IndexRef<'a> {
fn from(index: &'a MutableIndex) -> Self {
IndexRef::Mutable(index)
}
}
impl<'a> IndexRef<'a> {
pub fn num_commits(&self) -> u32 {
match self {
IndexRef::Readonly(index) => index.num_commits(),
IndexRef::Mutable(index) => index.num_commits(),
}
}
pub fn stats(&self) -> IndexStats {
match self {
IndexRef::Readonly(index) => index.stats(),
IndexRef::Mutable(index) => index.stats(),
}
}
pub fn commit_id_to_pos(&self, commit_id: &CommitId) -> Option<IndexPosition> {
match self {
IndexRef::Readonly(index) => index.commit_id_to_pos(commit_id),
IndexRef::Mutable(index) => index.commit_id_to_pos(commit_id),
}
}
pub fn shortest_unique_commit_id_prefix_len(&self, commit_id: &CommitId) -> usize {
match self {
IndexRef::Readonly(index) => index.shortest_unique_commit_id_prefix_len(commit_id),
IndexRef::Mutable(index) => index.shortest_unique_commit_id_prefix_len(commit_id),
}
}
pub fn resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution<CommitId> {
match self {
IndexRef::Readonly(index) => index.resolve_prefix(prefix),
IndexRef::Mutable(index) => index.resolve_prefix(prefix),
}
}
pub fn entry_by_id(&self, commit_id: &CommitId) -> Option<IndexEntry<'a>> {
match self {
IndexRef::Readonly(index) => index.entry_by_id(commit_id),
IndexRef::Mutable(index) => index.entry_by_id(commit_id),
}
}
pub fn entry_by_pos(&self, pos: IndexPosition) -> IndexEntry<'a> {
match self {
IndexRef::Readonly(index) => index.entry_by_pos(pos),
IndexRef::Mutable(index) => index.entry_by_pos(pos),
}
}
pub fn has_id(&self, commit_id: &CommitId) -> bool {
match self {
IndexRef::Readonly(index) => index.has_id(commit_id),
IndexRef::Mutable(index) => index.has_id(commit_id),
}
}
pub fn is_ancestor(&self, ancestor_id: &CommitId, descendant_id: &CommitId) -> bool {
match self {
IndexRef::Readonly(index) => index.is_ancestor(ancestor_id, descendant_id),
IndexRef::Mutable(index) => index.is_ancestor(ancestor_id, descendant_id),
}
}
pub fn common_ancestors(&self, set1: &[CommitId], set2: &[CommitId]) -> Vec<CommitId> {
match self {
IndexRef::Readonly(index) => index.common_ancestors(set1, set2),
IndexRef::Mutable(index) => index.common_ancestors(set1, set2),
}
}
pub fn walk_revs(&self, wanted: &[CommitId], unwanted: &[CommitId]) -> RevWalk<'a> {
match self {
IndexRef::Readonly(index) => index.walk_revs(wanted, unwanted),
IndexRef::Mutable(index) => index.walk_revs(wanted, unwanted),
}
}
pub fn heads(&self, candidates: &mut dyn Iterator<Item = &CommitId>) -> Vec<CommitId> {
match self {
IndexRef::Readonly(index) => index.heads(candidates),
IndexRef::Mutable(index) => index.heads(candidates),
}
}
}
struct CommitGraphEntry<'a> {
data: &'a [u8],
commit_id_length: usize,
@ -431,10 +326,6 @@ impl MutableIndex {
}
}
pub fn as_index_ref(&self) -> IndexRef {
IndexRef::Mutable(self)
}
pub fn add_commit(&mut self, commit: &Commit) {
self.add_commit_data(
commit.id().clone(),
@ -1624,10 +1515,6 @@ impl ReadonlyIndex {
}))
}
pub fn as_index_ref(self: &ReadonlyIndex) -> IndexRef {
IndexRef::Readonly(self)
}
pub fn name(&self) -> &str {
&self.name
}

View file

@ -13,11 +13,11 @@
// limitations under the License.
use crate::backend::CommitId;
use crate::index::IndexRef;
use crate::index::Index;
use crate::op_store::{BranchTarget, RefTarget};
pub fn merge_ref_targets(
index: IndexRef,
index: &dyn Index,
left: Option<&RefTarget>,
base: Option<&RefTarget>,
right: Option<&RefTarget>,
@ -63,7 +63,7 @@ pub fn merge_ref_targets(
}
fn find_pair_to_remove(
index: IndexRef,
index: &dyn Index,
adds: &[CommitId],
removes: &[CommitId],
) -> Option<(Option<usize>, usize)> {

View file

@ -30,8 +30,7 @@ use crate::commit_builder::CommitBuilder;
use crate::dag_walk::topo_order_reverse;
use crate::git_backend::GitBackend;
use crate::index::{
HexPrefix, Index, IndexEntry, IndexPosition, IndexRef, MutableIndex, PrefixResolution,
ReadonlyIndex,
HexPrefix, Index, IndexEntry, IndexPosition, MutableIndex, PrefixResolution, ReadonlyIndex,
};
use crate::index_store::IndexStore;
use crate::local_backend::LocalBackend;
@ -80,10 +79,10 @@ impl<'a> RepoRef<'a> {
}
}
pub fn index(&self) -> IndexRef<'a> {
pub fn index(&self) -> &'a dyn Index {
match self {
RepoRef::Readonly(repo) => IndexRef::Readonly(repo.index()),
RepoRef::Mutable(repo) => IndexRef::Mutable(repo.index()),
RepoRef::Readonly(repo) => repo.index().as_ref(),
RepoRef::Mutable(repo) => repo.index(),
}
}
@ -1080,7 +1079,7 @@ impl MutableRepo {
let base_target = base.get_ref(&ref_name);
let other_target = other.get_ref(&ref_name);
self.view.get_mut().merge_single_ref(
self.index.as_index_ref(),
&self.index,
&ref_name,
base_target.as_ref(),
other_target.as_ref(),
@ -1088,7 +1087,7 @@ impl MutableRepo {
}
if let Some(new_git_head) = merge_ref_targets(
self.index.as_index_ref(),
&self.index,
self.view().git_head(),
base.git_head(),
other.git_head(),
@ -1148,12 +1147,9 @@ impl MutableRepo {
base_target: Option<&RefTarget>,
other_target: Option<&RefTarget>,
) {
self.view.get_mut().merge_single_ref(
self.index.as_index_ref(),
ref_name,
base_target,
other_target,
);
self.view
.get_mut()
.merge_single_ref(&self.index, ref_name, base_target, other_target);
}
}

View file

@ -17,7 +17,7 @@ use std::collections::{BTreeMap, HashMap, HashSet};
use itertools::Itertools;
use crate::backend::CommitId;
use crate::index::IndexRef;
use crate::index::Index;
use crate::op_store;
use crate::op_store::{BranchTarget, RefTarget, WorkspaceId};
use crate::refs::merge_ref_targets;
@ -266,7 +266,7 @@ impl View {
pub fn merge_single_ref(
&mut self,
index: IndexRef,
index: &dyn Index,
ref_name: &RefName,
base_target: Option<&RefTarget>,
other_target: Option<&RefTarget>,

View file

@ -50,7 +50,7 @@ fn test_merge_ref_targets() {
let _target7 = RefTarget::Normal(commit7.id().clone());
let index = repo.index();
let index_ref = index.as_index_ref();
let index_ref = index.as_ref();
// Left moved forward
assert_eq!(