diff --git a/cli/src/commands/debug.rs b/cli/src/commands/debug.rs index c6f3cb8f5..fe5bf02f4 100644 --- a/cli/src/commands/debug.rs +++ b/cli/src/commands/debug.rs @@ -18,7 +18,7 @@ use std::io::Write as _; use clap::Subcommand; use jj_lib::backend::ObjectId; -use jj_lib::default_index::{DefaultIndexStore, DefaultReadonlyIndex}; +use jj_lib::default_index::{AsCompositeIndex as _, DefaultIndexStore, DefaultReadonlyIndex}; use jj_lib::local_working_copy::LocalWorkingCopy; use jj_lib::revset; use jj_lib::working_copy::WorkingCopy; diff --git a/lib/src/default_index/composite.rs b/lib/src/default_index/composite.rs index 97551fee0..88cdc0dd8 100644 --- a/lib/src/default_index/composite.rs +++ b/lib/src/default_index/composite.rs @@ -63,6 +63,26 @@ pub(super) trait IndexSegment: Send + Sync { fn segment_entry_by_pos(&self, pos: IndexPosition, local_pos: u32) -> IndexEntry; } +/// Abstraction over owned and borrowed types that can be cheaply converted to +/// a `CompositeIndex` reference. +pub trait AsCompositeIndex { + /// Returns reference wrapper that provides global access to this index. + fn as_composite(&self) -> CompositeIndex<'_>; +} + +impl AsCompositeIndex for &T { + fn as_composite(&self) -> CompositeIndex<'_> { + ::as_composite(self) + } +} + +impl AsCompositeIndex for &mut T { + fn as_composite(&self) -> CompositeIndex<'_> { + ::as_composite(self) + } +} + +/// Reference wrapper that provides global access to nested index segments. #[derive(Clone, Copy)] pub struct CompositeIndex<'a>(&'a dyn IndexSegment); @@ -288,6 +308,12 @@ impl<'a> CompositeIndex<'a> { } } +impl AsCompositeIndex for CompositeIndex<'_> { + fn as_composite(&self) -> CompositeIndex<'_> { + *self + } +} + impl Index for CompositeIndex<'_> { /// Suppose the given `commit_id` exists, returns the minimum prefix length /// to disambiguate it. The length to be returned is a number of hexadecimal diff --git a/lib/src/default_index/mod.rs b/lib/src/default_index/mod.rs index 4db14c927..674eaffd9 100644 --- a/lib/src/default_index/mod.rs +++ b/lib/src/default_index/mod.rs @@ -21,7 +21,7 @@ mod readonly; mod rev_walk; mod store; -pub use self::composite::{CompositeIndex, IndexLevelStats, IndexStats}; +pub use self::composite::{AsCompositeIndex, CompositeIndex, IndexLevelStats, IndexStats}; pub use self::entry::{IndexEntry, IndexPosition}; pub use self::mutable::DefaultMutableIndex; pub use self::readonly::DefaultReadonlyIndex; diff --git a/lib/src/default_index/mutable.rs b/lib/src/default_index/mutable.rs index 11c12c636..de8cf9b85 100644 --- a/lib/src/default_index/mutable.rs +++ b/lib/src/default_index/mutable.rs @@ -30,7 +30,7 @@ use itertools::Itertools; use smallvec::SmallVec; use tempfile::NamedTempFile; -use super::composite::{CompositeIndex, IndexSegment}; +use super::composite::{AsCompositeIndex, CompositeIndex, IndexSegment}; use super::entry::{IndexEntry, IndexPosition, SmallIndexPositionsVec}; use super::readonly::{DefaultReadonlyIndex, ReadonlyIndexSegment}; use super::store::IndexLoadError; @@ -403,10 +403,6 @@ impl DefaultMutableIndex { DefaultMutableIndex(mutable_segment) } - pub fn as_composite(&self) -> CompositeIndex { - self.0.as_composite() - } - #[cfg(test)] pub(crate) fn add_commit_data( &mut self, @@ -422,6 +418,12 @@ impl DefaultMutableIndex { } } +impl AsCompositeIndex for DefaultMutableIndex { + fn as_composite(&self) -> CompositeIndex<'_> { + self.0.as_composite() + } +} + impl Index for DefaultMutableIndex { fn shortest_unique_commit_id_prefix_len(&self, commit_id: &CommitId) -> usize { self.as_composite() diff --git a/lib/src/default_index/readonly.rs b/lib/src/default_index/readonly.rs index 10b822627..e08895ca0 100644 --- a/lib/src/default_index/readonly.rs +++ b/lib/src/default_index/readonly.rs @@ -25,7 +25,7 @@ use std::sync::Arc; use byteorder::{LittleEndian, ReadBytesExt}; use smallvec::SmallVec; -use super::composite::{CompositeIndex, IndexSegment}; +use super::composite::{AsCompositeIndex, CompositeIndex, IndexSegment}; use super::entry::{IndexEntry, IndexPosition, SmallIndexPositionsVec}; use super::mutable::DefaultMutableIndex; use super::store::IndexLoadError; @@ -384,8 +384,10 @@ impl DefaultReadonlyIndex { pub(super) fn as_segment(&self) -> &Arc { &self.0 } +} - pub fn as_composite(&self) -> CompositeIndex { +impl AsCompositeIndex for DefaultReadonlyIndex { + fn as_composite(&self) -> CompositeIndex<'_> { self.0.as_composite() } } diff --git a/lib/src/default_revset_engine.rs b/lib/src/default_revset_engine.rs index 3e91ab37c..1071a4483 100644 --- a/lib/src/default_revset_engine.rs +++ b/lib/src/default_revset_engine.rs @@ -978,7 +978,7 @@ fn has_diff_from_parent( mod tests { use super::*; use crate::backend::{ChangeId, CommitId, ObjectId}; - use crate::default_index::DefaultMutableIndex; + use crate::default_index::{AsCompositeIndex as _, DefaultMutableIndex}; /// Generator of unique 16-byte ChangeId excluding root id fn change_id_generator() -> impl FnMut() -> ChangeId { diff --git a/lib/tests/test_default_revset_graph_iterator.rs b/lib/tests/test_default_revset_graph_iterator.rs index 95b412de7..dbd6a5ef7 100644 --- a/lib/tests/test_default_revset_graph_iterator.rs +++ b/lib/tests/test_default_revset_graph_iterator.rs @@ -14,7 +14,7 @@ use itertools::Itertools; use jj_lib::commit::Commit; -use jj_lib::default_index::DefaultReadonlyIndex; +use jj_lib::default_index::{AsCompositeIndex as _, DefaultReadonlyIndex}; use jj_lib::default_revset_engine::{evaluate, RevsetImpl}; use jj_lib::repo::{ReadonlyRepo, Repo as _}; use jj_lib::revset::ResolvedExpression; diff --git a/lib/tests/test_index.rs b/lib/tests/test_index.rs index 99b410dd2..e1f8b7968 100644 --- a/lib/tests/test_index.rs +++ b/lib/tests/test_index.rs @@ -18,7 +18,7 @@ use jj_lib::backend::CommitId; use jj_lib::commit::Commit; use jj_lib::commit_builder::CommitBuilder; use jj_lib::default_index::{ - CompositeIndex, DefaultMutableIndex, DefaultReadonlyIndex, IndexPosition, + AsCompositeIndex as _, CompositeIndex, DefaultMutableIndex, DefaultReadonlyIndex, IndexPosition, }; use jj_lib::index::Index as _; use jj_lib::repo::{MutableRepo, ReadonlyRepo, Repo};