Skip to content

Commit

Permalink
repo, workspace: remove 'static lifetime bound from initializer funct…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
yuja committed Dec 2, 2023
1 parent 48d586c commit 1db0335
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 43 deletions.
2 changes: 1 addition & 1 deletion cli/examples/custom-working-copy/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl ConflictsWorkingCopy {
})
}

fn initializer() -> Box<WorkingCopyInitializer> {
fn initializer() -> Box<WorkingCopyInitializer<'static>> {
Box::new(
|store, working_copy_path, state_path, workspace_id, operation_id| {
let wc = Self::init(
Expand Down
21 changes: 11 additions & 10 deletions lib/src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,22 @@ pub enum RepoInitError {
}

impl ReadonlyRepo {
pub fn default_op_store_initializer() -> &'static OpStoreInitializer {
pub fn default_op_store_initializer() -> &'static OpStoreInitializer<'static> {
&|_settings, store_path| Box::new(SimpleOpStore::init(store_path))
}

pub fn default_op_heads_store_initializer() -> &'static OpHeadsStoreInitializer {
pub fn default_op_heads_store_initializer() -> &'static OpHeadsStoreInitializer<'static> {
&|_settings, store_path| {
let store = SimpleOpHeadsStore::init(store_path);
Box::new(store)
}
}

pub fn default_index_store_initializer() -> &'static IndexStoreInitializer {
pub fn default_index_store_initializer() -> &'static IndexStoreInitializer<'static> {
&|_settings, store_path| Box::new(DefaultIndexStore::init(store_path))
}

pub fn default_submodule_store_initializer() -> &'static SubmoduleStoreInitializer {
pub fn default_submodule_store_initializer() -> &'static SubmoduleStoreInitializer<'static> {
&|_settings, store_path| Box::new(DefaultSubmoduleStore::init(store_path))
}

Expand Down Expand Up @@ -343,12 +343,13 @@ impl Repo for ReadonlyRepo {
}
}

pub type BackendInitializer =
dyn Fn(&UserSettings, &Path) -> Result<Box<dyn Backend>, BackendInitError>;
pub type OpStoreInitializer = dyn Fn(&UserSettings, &Path) -> Box<dyn OpStore>;
pub type OpHeadsStoreInitializer = dyn Fn(&UserSettings, &Path) -> Box<dyn OpHeadsStore>;
pub type IndexStoreInitializer = dyn Fn(&UserSettings, &Path) -> Box<dyn IndexStore>;
pub type SubmoduleStoreInitializer = dyn Fn(&UserSettings, &Path) -> Box<dyn SubmoduleStore>;
pub type BackendInitializer<'a> =
dyn Fn(&UserSettings, &Path) -> Result<Box<dyn Backend>, BackendInitError> + 'a;
pub type OpStoreInitializer<'a> = dyn Fn(&UserSettings, &Path) -> Box<dyn OpStore> + 'a;
pub type OpHeadsStoreInitializer<'a> = dyn Fn(&UserSettings, &Path) -> Box<dyn OpHeadsStore> + 'a;
pub type IndexStoreInitializer<'a> = dyn Fn(&UserSettings, &Path) -> Box<dyn IndexStore> + 'a;
pub type SubmoduleStoreInitializer<'a> =
dyn Fn(&UserSettings, &Path) -> Box<dyn SubmoduleStore> + 'a;

type BackendFactory =
Box<dyn Fn(&UserSettings, &Path) -> Result<Box<dyn Backend>, BackendLoadError>>;
Expand Down
55 changes: 26 additions & 29 deletions lib/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl Workspace {
user_settings: &UserSettings,
workspace_root: &Path,
) -> Result<(Self, Arc<ReadonlyRepo>), WorkspaceInitError> {
let backend_initializer: &'static BackendInitializer =
let backend_initializer: &BackendInitializer =
&|_settings, store_path| Ok(Box::new(LocalBackend::init(store_path)));
let signer = Signer::from_settings(user_settings)?;
Self::init_with_backend(user_settings, workspace_root, backend_initializer, signer)
Expand All @@ -159,7 +159,7 @@ impl Workspace {
user_settings: &UserSettings,
workspace_root: &Path,
) -> Result<(Self, Arc<ReadonlyRepo>), WorkspaceInitError> {
let backend_initializer: &'static BackendInitializer =
let backend_initializer: &BackendInitializer =
&|settings, store_path| Ok(Box::new(GitBackend::init_internal(settings, store_path)?));
let signer = Signer::from_settings(user_settings)?;
Self::init_with_backend(user_settings, workspace_root, backend_initializer, signer)
Expand All @@ -171,25 +171,21 @@ impl Workspace {
user_settings: &UserSettings,
workspace_root: &Path,
) -> Result<(Self, Arc<ReadonlyRepo>), WorkspaceInitError> {
let backend_initializer = {
let workspace_root = workspace_root.to_owned();
move |settings: &UserSettings, store_path: &Path| -> Result<Box<dyn Backend>, _> {
// TODO: Clean up path normalization. store_path is canonicalized by
// ReadonlyRepo::init(). workspace_root will be canonicalized by
// Workspace::new(), but it's not yet here.
let store_relative_workspace_root =
if let Ok(workspace_root) = workspace_root.canonicalize() {
file_util::relative_path(store_path, &workspace_root)
} else {
workspace_root.to_owned()
};
let backend = GitBackend::init_colocated(
settings,
store_path,
&store_relative_workspace_root,
)?;
Ok(Box::new(backend))
}
let backend_initializer = |settings: &UserSettings,
store_path: &Path|
-> Result<Box<dyn Backend>, _> {
// TODO: Clean up path normalization. store_path is canonicalized by
// ReadonlyRepo::init(). workspace_root will be canonicalized by
// Workspace::new(), but it's not yet here.
let store_relative_workspace_root =
if let Ok(workspace_root) = workspace_root.canonicalize() {
file_util::relative_path(store_path, &workspace_root)
} else {
workspace_root.to_owned()
};
let backend =
GitBackend::init_colocated(settings, store_path, &store_relative_workspace_root)?;
Ok(Box::new(backend))
};
let signer = Signer::from_settings(user_settings)?;
Self::init_with_backend(user_settings, workspace_root, &backend_initializer, signer)
Expand Down Expand Up @@ -502,7 +498,7 @@ impl WorkspaceLoader {
}
}

pub fn default_working_copy_initializer() -> &'static WorkingCopyInitializer {
pub fn default_working_copy_initializer() -> &'static WorkingCopyInitializer<'static> {
&|store: Arc<Store>, working_copy_path, state_path, workspace_id, operation_id| {
let wc = LocalWorkingCopy::init(
store,
Expand Down Expand Up @@ -530,11 +526,12 @@ pub fn default_working_copy_factories() -> HashMap<String, WorkingCopyFactory> {
factories
}

pub type WorkingCopyInitializer = dyn Fn(
Arc<Store>,
PathBuf,
PathBuf,
WorkspaceId,
OperationId,
) -> Result<Box<dyn WorkingCopy>, WorkingCopyStateError>;
pub type WorkingCopyInitializer<'a> = dyn Fn(
Arc<Store>,
PathBuf,
PathBuf,
WorkspaceId,
OperationId,
) -> Result<Box<dyn WorkingCopy>, WorkingCopyStateError>
+ 'a;
pub type WorkingCopyFactory = Box<dyn Fn(&Arc<Store>, &Path, &Path) -> Box<dyn WorkingCopy>>;
6 changes: 3 additions & 3 deletions lib/tests/test_git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,7 @@ impl GitRepoData {
let repo = ReadonlyRepo::init(
&settings,
&jj_repo_dir,
&move |settings, store_path| {
&|settings, store_path| {
Ok(Box::new(GitBackend::init_external(
settings,
store_path,
Expand Down Expand Up @@ -1985,7 +1985,7 @@ fn test_init() {
let repo = &ReadonlyRepo::init(
&settings,
&jj_repo_dir,
&move |settings, store_path| {
&|settings, store_path| {
Ok(Box::new(GitBackend::init_external(
settings,
store_path,
Expand Down Expand Up @@ -2311,7 +2311,7 @@ fn set_up_push_repos(settings: &UserSettings, temp_dir: &TempDir) -> PushTestSet
let jj_repo = ReadonlyRepo::init(
settings,
&jj_repo_dir,
&move |settings, store_path| {
&|settings, store_path| {
Ok(Box::new(GitBackend::init_external(
settings,
store_path,
Expand Down

0 comments on commit 1db0335

Please sign in to comment.