diff --git a/cli/src/merge_tools/builtin.rs b/cli/src/merge_tools/builtin.rs index a124026c54..027a996e1d 100644 --- a/cli/src/merge_tools/builtin.rs +++ b/cli/src/merge_tools/builtin.rs @@ -601,8 +601,7 @@ pub fn edit_merge_builtin( path: &RepoPath, content: Merge, ) -> Result { - let slices = content.map(|ContentHunk(v)| v.as_slice()); - let merge_result = files::merge(&slices); + let merge_result = files::merge(&content); let sections = make_merge_sections(merge_result)?; let mut input = scm_record::helpers::CrosstermInput; let recorder = scm_record::Recorder::new( @@ -1014,8 +1013,7 @@ mod tests { let content = extract_as_single_hunk(&merge, store, path) .block_on() .unwrap(); - let slices = content.map(|ContentHunk(buf)| buf.as_slice()); - let merge_result = files::merge(&slices); + let merge_result = files::merge(&content); let sections = make_merge_sections(merge_result).unwrap(); insta::assert_debug_snapshot!(sections, @r###" [ diff --git a/lib/src/conflicts.rs b/lib/src/conflicts.rs index f8d586aa7e..fa40a0e1c9 100644 --- a/lib/src/conflicts.rs +++ b/lib/src/conflicts.rs @@ -218,8 +218,7 @@ pub fn materialize_merge_result( single_hunk: &Merge, output: &mut dyn Write, ) -> std::io::Result<()> { - let slices = single_hunk.map(|content| content.0.as_slice()); - let merge_result = files::merge(&slices); + let merge_result = files::merge(single_hunk); match merge_result { MergeResult::Resolved(content) => { output.write_all(&content.0)?; diff --git a/lib/src/files.rs b/lib/src/files.rs index d623efc9b3..2c88b3fa35 100644 --- a/lib/src/files.rs +++ b/lib/src/files.rs @@ -141,9 +141,16 @@ impl<'a> Iterator for DiffLineIterator<'a> { } } +// TODO: Maybe ContentHunk can be replaced with BString? #[derive(PartialEq, Eq, Clone)] pub struct ContentHunk(pub Vec); +impl AsRef<[u8]> for ContentHunk { + fn as_ref(&self) -> &[u8] { + self.0.as_ref() + } +} + impl Debug for ContentHunk { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { String::from_utf8_lossy(&self.0).fmt(f) @@ -156,7 +163,7 @@ pub enum MergeResult { Conflict(Vec>), } -pub fn merge(slices: &Merge<&[u8]>) -> MergeResult { +pub fn merge>(slices: &Merge) -> MergeResult { // TODO: Using the first remove as base (first in the inputs) is how it's // usually done for 3-way conflicts. Are there better heuristics when there are // more than 3 parts? @@ -213,7 +220,7 @@ mod tests { } fn merge(removes: &[&[u8]], adds: &[&[u8]]) -> MergeResult { - super::merge(&Merge::from_removes_adds(removes.to_vec(), adds.to_vec())) + super::merge(&Merge::from_removes_adds(removes, adds)) } #[test] diff --git a/lib/src/tree.rs b/lib/src/tree.rs index 8627014b22..2d4a4d86b0 100644 --- a/lib/src/tree.rs +++ b/lib/src/tree.rs @@ -454,8 +454,7 @@ pub fn try_resolve_file_conflict( })?; Ok(content) })?; - let slices = contents.map(|content| content.as_slice()); - let merge_result = files::merge(&slices); + let merge_result = files::merge(&contents); match merge_result { MergeResult::Resolved(merged_content) => { let id = store.write_file(filename, &mut merged_content.0.as_slice())?;