From c8be8c3edd1b5db034d6a70fc86b8b7acc299521 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Sun, 10 Mar 2024 11:18:00 +0900 Subject: [PATCH] index: add type alias for "dyn IndexSegment" to clarify it's 'static This helps to migrate CompositeIndex<'_> wrapper to &CompositeIndex. If the wrapped reference had a lifetimed field, it couldn't be represented as a trivial reference type. --- lib/src/default_index/composite.rs | 10 ++++++---- lib/src/default_index/entry.rs | 6 +++--- lib/src/default_index/mod.rs | 10 +++++----- lib/src/default_index/mutable.rs | 6 ++++-- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/lib/src/default_index/composite.rs b/lib/src/default_index/composite.rs index 39fe1565a..2da1e4dc0 100644 --- a/lib/src/default_index/composite.rs +++ b/lib/src/default_index/composite.rs @@ -76,6 +76,8 @@ pub(super) trait IndexSegment: Send + Sync { fn parent_positions(&self, local_pos: LocalPosition) -> SmallIndexPositionsVec; } +pub(super) type DynIndexSegment = dyn IndexSegment; + /// Abstraction over owned and borrowed types that can be cheaply converted to /// a `CompositeIndex` reference. pub trait AsCompositeIndex { @@ -97,10 +99,10 @@ impl AsCompositeIndex for &mut T { /// Reference wrapper that provides global access to nested index segments. #[derive(Clone, Copy)] -pub struct CompositeIndex<'a>(&'a dyn IndexSegment); +pub struct CompositeIndex<'a>(&'a DynIndexSegment); impl<'a> CompositeIndex<'a> { - pub(super) fn new(segment: &'a dyn IndexSegment) -> Self { + pub(super) fn new(segment: &'a DynIndexSegment) -> Self { CompositeIndex(segment) } @@ -113,10 +115,10 @@ impl<'a> CompositeIndex<'a> { } /// Iterates self and its ancestor index segments. - pub(super) fn ancestor_index_segments(&self) -> impl Iterator { + pub(super) fn ancestor_index_segments(&self) -> impl Iterator { iter::once(self.0).chain( self.ancestor_files_without_local() - .map(|file| file.as_ref() as &dyn IndexSegment), + .map(|file| file.as_ref() as &DynIndexSegment), ) } diff --git a/lib/src/default_index/entry.rs b/lib/src/default_index/entry.rs index 56a0d630f..7cfbfdfa3 100644 --- a/lib/src/default_index/entry.rs +++ b/lib/src/default_index/entry.rs @@ -19,7 +19,7 @@ use std::hash::{Hash, Hasher}; use smallvec::SmallVec; -use super::composite::{CompositeIndex, IndexSegment}; +use super::composite::{CompositeIndex, DynIndexSegment}; use crate::backend::{ChangeId, CommitId}; use crate::object_id::ObjectId; @@ -43,7 +43,7 @@ pub(super) type SmallLocalPositionsVec = SmallVec<[LocalPosition; 4]>; #[derive(Clone)] pub struct IndexEntry<'a> { - source: &'a dyn IndexSegment, + source: &'a DynIndexSegment, pos: IndexPosition, /// Position within the source segment local_pos: LocalPosition, @@ -75,7 +75,7 @@ impl Hash for IndexEntry<'_> { impl<'a> IndexEntry<'a> { pub(super) fn new( - source: &'a dyn IndexSegment, + source: &'a DynIndexSegment, pos: IndexPosition, local_pos: LocalPosition, ) -> Self { diff --git a/lib/src/default_index/mod.rs b/lib/src/default_index/mod.rs index 5bb8610a1..cd833b78c 100644 --- a/lib/src/default_index/mod.rs +++ b/lib/src/default_index/mod.rs @@ -37,7 +37,7 @@ mod tests { use smallvec::smallvec_inline; use test_case::test_case; - use super::composite::IndexSegment; + use super::composite::{DynIndexSegment, IndexSegment}; use super::entry::SmallIndexPositionsVec; use super::mutable::MutableIndexSegment; use super::*; @@ -63,7 +63,7 @@ mod tests { fn index_empty(on_disk: bool) { let temp_dir = testutils::new_temp_dir(); let mutable_segment = MutableIndexSegment::full(3, 16); - let index_segment: Box = if on_disk { + let index_segment: Box = if on_disk { let saved_index = mutable_segment.save_in(temp_dir.path()).unwrap(); Box::new(Arc::try_unwrap(saved_index).unwrap()) } else { @@ -94,7 +94,7 @@ mod tests { let id_0 = CommitId::from_hex("000000"); let change_id0 = new_change_id(); mutable_segment.add_commit_data(id_0.clone(), change_id0.clone(), &[]); - let index_segment: Box = if on_disk { + let index_segment: Box = if on_disk { let saved_index = mutable_segment.save_in(temp_dir.path()).unwrap(); Box::new(Arc::try_unwrap(saved_index).unwrap()) } else { @@ -179,7 +179,7 @@ mod tests { mutable_segment.add_commit_data(id_3.clone(), change_id3.clone(), &[id_2.clone()]); mutable_segment.add_commit_data(id_4.clone(), change_id4, &[id_1.clone()]); mutable_segment.add_commit_data(id_5.clone(), change_id5, &[id_4.clone(), id_2.clone()]); - let index_segment: Box = if on_disk { + let index_segment: Box = if on_disk { let saved_index = mutable_segment.save_in(temp_dir.path()).unwrap(); Box::new(Arc::try_unwrap(saved_index).unwrap()) } else { @@ -291,7 +291,7 @@ mod tests { new_change_id(), &[id_1, id_2, id_3, id_4, id_5], ); - let index_segment: Box = if on_disk { + let index_segment: Box = if on_disk { let saved_index = mutable_segment.save_in(temp_dir.path()).unwrap(); Box::new(Arc::try_unwrap(saved_index).unwrap()) } else { diff --git a/lib/src/default_index/mutable.rs b/lib/src/default_index/mutable.rs index f88c121c5..9d17565a7 100644 --- a/lib/src/default_index/mutable.rs +++ b/lib/src/default_index/mutable.rs @@ -29,7 +29,9 @@ use itertools::Itertools; use smallvec::{smallvec, SmallVec}; use tempfile::NamedTempFile; -use super::composite::{AsCompositeIndex, ChangeIdIndexImpl, CompositeIndex, IndexSegment}; +use super::composite::{ + AsCompositeIndex, ChangeIdIndexImpl, CompositeIndex, DynIndexSegment, IndexSegment, +}; use super::entry::{IndexPosition, LocalPosition, SmallIndexPositionsVec, SmallLocalPositionsVec}; use super::readonly::{ DefaultReadonlyIndex, ReadonlyIndexSegment, INDEX_SEGMENT_FILE_FORMAT_VERSION, OVERFLOW_FLAG, @@ -137,7 +139,7 @@ impl MutableIndexSegment { self.graph.push(entry); } - pub(super) fn add_commits_from(&mut self, other_segment: &dyn IndexSegment) { + pub(super) fn add_commits_from(&mut self, other_segment: &DynIndexSegment) { let other = CompositeIndex::new(other_segment); for pos in other_segment.num_parent_commits()..other.num_commits() { let entry = other.entry_by_pos(IndexPosition(pos));