diff --git a/cli/examples/custom-backend/main.rs b/cli/examples/custom-backend/main.rs index 4bc1c797e..6cf0a0f25 100644 --- a/cli/examples/custom-backend/main.rs +++ b/cli/examples/custom-backend/main.rs @@ -166,7 +166,7 @@ impl Backend for JitBackend { fn write_commit( &self, contents: Commit, - sign_with: Option, + sign_with: Option<&mut SigningFn>, ) -> BackendResult<(CommitId, Commit)> { self.inner.write_commit(contents, sign_with) } diff --git a/lib/src/backend.rs b/lib/src/backend.rs index 68ec07209..a81c183ea 100644 --- a/lib/src/backend.rs +++ b/lib/src/backend.rs @@ -148,7 +148,7 @@ content_hash! { } } -pub type SigningFn = Box SignResult>>; +pub type SigningFn<'a> = dyn FnMut(&[u8]) -> SignResult> + 'a; /// Identifies a single legacy tree, which may have path-level conflicts, or a /// merge of multiple trees, where the individual trees do not have conflicts. @@ -531,6 +531,6 @@ pub trait Backend: Send + Sync + Debug { fn write_commit( &self, contents: Commit, - sign_with: Option, + sign_with: Option<&mut SigningFn>, ) -> BackendResult<(CommitId, Commit)>; } diff --git a/lib/src/commit_builder.rs b/lib/src/commit_builder.rs index 9f816e29e..67c50e48a 100644 --- a/lib/src/commit_builder.rs +++ b/lib/src/commit_builder.rs @@ -183,14 +183,13 @@ impl CommitBuilder<'_> { } } - let sign_settings = self.sign_settings; + let sign_settings = &self.sign_settings; let store = self.mut_repo.store(); - let signing_fn = (store.signer().can_sign() && sign_settings.should_sign(&self.commit)) - .then(|| { + let mut signing_fn = (store.signer().can_sign() && sign_settings.should_sign(&self.commit)) + .then(|| -> Box { let store = store.clone(); Box::new(move |data: &_| store.signer().sign(data, sign_settings.key.as_deref())) - as SigningFn }); // Commit backend doesn't use secure_sig for writing and enforces it with an @@ -198,7 +197,9 @@ impl CommitBuilder<'_> { // if we're rewriting a signed commit self.commit.secure_sig = None; - let commit = self.mut_repo.write_commit(self.commit, signing_fn)?; + let commit = self + .mut_repo + .write_commit(self.commit, signing_fn.as_deref_mut())?; if let Some(rewrite_source_id) = rewrite_source_id { self.mut_repo .record_rewritten_commit(rewrite_source_id, commit.id().clone()) diff --git a/lib/src/git_backend.rs b/lib/src/git_backend.rs index 866482f69..e0126a93a 100644 --- a/lib/src/git_backend.rs +++ b/lib/src/git_backend.rs @@ -904,7 +904,7 @@ impl Backend for GitBackend { fn write_commit( &self, mut contents: Commit, - mut sign_with: Option, + mut sign_with: Option<&mut SigningFn>, ) -> BackendResult<(CommitId, Commit)> { assert!(contents.secure_sig.is_none(), "commit.secure_sig was set"); @@ -1668,12 +1668,14 @@ mod tests { secure_sig: None, }; - let signer = Box::new(|data: &_| { + let mut signer = |data: &_| { let hash: String = blake2b_hash(data).encode_hex(); Ok(format!("test sig\n\n\nhash={hash}").into_bytes()) - }); + }; - let (id, commit) = backend.write_commit(commit, Some(signer)).unwrap(); + let (id, commit) = backend + .write_commit(commit, Some(&mut signer as &mut SigningFn)) + .unwrap(); let git_repo = backend.git_repo(); let obj = git_repo.find_object(id.as_bytes()).unwrap(); diff --git a/lib/src/local_backend.rs b/lib/src/local_backend.rs index c2283e26e..09574b592 100644 --- a/lib/src/local_backend.rs +++ b/lib/src/local_backend.rs @@ -267,7 +267,7 @@ impl Backend for LocalBackend { fn write_commit( &self, mut commit: Commit, - sign_with: Option, + sign_with: Option<&mut SigningFn>, ) -> BackendResult<(CommitId, Commit)> { assert!(commit.secure_sig.is_none(), "commit.secure_sig was set"); @@ -279,7 +279,7 @@ impl Backend for LocalBackend { let temp_file = NamedTempFile::new_in(&self.path).map_err(to_other_err)?; let mut proto = commit_to_proto(&commit); - if let Some(mut sign) = sign_with { + if let Some(sign) = sign_with { let data = proto.encode_to_vec(); let sig = sign(&data).map_err(to_other_err)?; proto.secure_sig = Some(sig.clone()); diff --git a/lib/src/repo.rs b/lib/src/repo.rs index 2765766ff..135927b04 100644 --- a/lib/src/repo.rs +++ b/lib/src/repo.rs @@ -801,7 +801,7 @@ impl MutableRepo { pub fn write_commit( &mut self, commit: backend::Commit, - sign_with: Option, + sign_with: Option<&mut SigningFn>, ) -> BackendResult { let commit = self.store().write_commit(commit, sign_with)?; self.add_head(&commit); diff --git a/lib/src/store.rs b/lib/src/store.rs index 0ab0149ce..f4810bd36 100644 --- a/lib/src/store.rs +++ b/lib/src/store.rs @@ -138,7 +138,7 @@ impl Store { pub fn write_commit( self: &Arc, commit: backend::Commit, - sign_with: Option, + sign_with: Option<&mut SigningFn>, ) -> BackendResult { assert!(!commit.parents.is_empty()); diff --git a/lib/testutils/src/test_backend.rs b/lib/testutils/src/test_backend.rs index a3d63aeae..59ab5037d 100644 --- a/lib/testutils/src/test_backend.rs +++ b/lib/testutils/src/test_backend.rs @@ -276,7 +276,7 @@ impl Backend for TestBackend { fn write_commit( &self, mut contents: Commit, - mut sign_with: Option, + mut sign_with: Option<&mut SigningFn>, ) -> BackendResult<(CommitId, Commit)> { assert!(contents.secure_sig.is_none(), "commit.secure_sig was set");