diff --git a/cli/src/commands/chmod.rs b/cli/src/commands/chmod.rs index 4f361f969e..e4204c06e0 100644 --- a/cli/src/commands/chmod.rs +++ b/cli/src/commands/chmod.rs @@ -91,7 +91,6 @@ pub(crate) fn cmd_chmod( } let all_files = tree_value .adds() - .iter() .flatten() .all(|tree_value| matches!(tree_value, TreeValue::File { .. })); if !all_files { diff --git a/cli/src/commands/resolve.rs b/cli/src/commands/resolve.rs index 8b62092d5a..065d05c1ba 100644 --- a/cli/src/commands/resolve.rs +++ b/cli/src/commands/resolve.rs @@ -142,7 +142,7 @@ pub(crate) fn print_conflicted_paths( for ((_, conflict), formatted_path) in std::iter::zip(conflicts.iter(), formatted_paths) { let sides = conflict.num_sides(); - let n_adds = conflict.adds().iter().flatten().count(); + let n_adds = conflict.adds().flatten().count(); let deletions = sides - n_adds; let mut seen_objects = BTreeMap::new(); // Sort for consistency and easier testing @@ -159,7 +159,7 @@ pub(crate) fn print_conflicted_paths( // TODO: We might decide it's OK for `jj resolve` to ignore special files in the // `removes` of a conflict (see e.g. https://github.com/martinvonz/jj/pull/978). In // that case, `conflict.removes` should be removed below. - for term in itertools::chain(conflict.removes().iter(), conflict.adds().iter()).flatten() { + for term in itertools::chain(conflict.removes(), conflict.adds()).flatten() { seen_objects.insert( match term { TreeValue::File { diff --git a/lib/src/conflicts.rs b/lib/src/conflicts.rs index 2b2c006118..3c06564d9f 100644 --- a/lib/src/conflicts.rs +++ b/lib/src/conflicts.rs @@ -161,7 +161,7 @@ pub fn materialize_merge_result( } // Emit the remaining positive terms as snapshots. - for slice in &hunk.adds()[add_index..] { + for slice in hunk.adds().skip(add_index) { output.write_all(CONFLICT_PLUS_LINE)?; output.write_all(&slice.0)?; } diff --git a/lib/src/git_backend.rs b/lib/src/git_backend.rs index 0017d98149..36057de1b3 100644 --- a/lib/src/git_backend.rs +++ b/lib/src/git_backend.rs @@ -940,12 +940,10 @@ fn write_tree_conflict( let entries = itertools::chain( conflict .removes() - .iter() .enumerate() .map(|(i, tree_id)| (format!(".jjconflict-base-{i}"), tree_id)), conflict .adds() - .iter() .enumerate() .map(|(i, tree_id)| (format!(".jjconflict-side-{i}"), tree_id)), ) diff --git a/lib/src/merge.rs b/lib/src/merge.rs index cd56586037..5e51f44a0b 100644 --- a/lib/src/merge.rs +++ b/lib/src/merge.rs @@ -161,13 +161,13 @@ impl Merge { } /// The removed values, also called negative terms. - pub fn removes(&self) -> &[T] { - &self.removes + pub fn removes(&self) -> impl ExactSizeIterator { + self.removes.iter() } /// The added values, also called positive terms. - pub fn adds(&self) -> &[T] { - &self.adds + pub fn adds(&self) -> impl ExactSizeIterator { + self.adds.iter() } /// Returns the zeroth added value, which is guaranteed to exist. @@ -427,8 +427,8 @@ impl Merge> { impl ContentHash for Merge { fn hash(&self, state: &mut impl digest::Update) { - self.removes().hash(state); - self.adds().hash(state); + self.removes.hash(state); + self.adds.hash(state); } } @@ -507,10 +507,10 @@ impl MergedTreeValue { /// Give a summary description of the conflict's "removes" and "adds" pub fn describe(&self, file: &mut dyn Write) -> std::io::Result<()> { file.write_all(b"Conflict:\n")?; - for term in self.removes().iter().flatten() { + for term in self.removes().flatten() { file.write_all(format!(" Removing {}\n", describe_conflict_term(term)).as_bytes())?; } - for term in self.adds().iter().flatten() { + for term in self.adds().flatten() { file.write_all(format!(" Adding {}\n", describe_conflict_term(term)).as_bytes())?; } Ok(()) diff --git a/lib/src/merged_tree.rs b/lib/src/merged_tree.rs index 282b867f60..b8708a970c 100644 --- a/lib/src/merged_tree.rs +++ b/lib/src/merged_tree.rs @@ -132,10 +132,10 @@ impl MergedTree { adds[i + 1].remove(path.clone()); } // Now add the terms that were present in the conflict to the appropriate trees. - for (i, term) in conflict.removes().iter().enumerate() { + for (i, term) in conflict.removes().enumerate() { removes[i].set_or_remove(path.clone(), term.clone()); } - for (i, term) in conflict.adds().iter().enumerate() { + for (i, term) in conflict.adds().enumerate() { adds[i].set_or_remove(path.clone(), term.clone()); } } diff --git a/lib/src/op_store.rs b/lib/src/op_store.rs index 8870d98c52..5660ba7c97 100644 --- a/lib/src/op_store.rs +++ b/lib/src/op_store.rs @@ -128,11 +128,11 @@ impl RefTarget { } pub fn removed_ids(&self) -> impl Iterator { - self.merge.removes().iter().flatten() + self.merge.removes().flatten() } pub fn added_ids(&self) -> impl Iterator { - self.merge.adds().iter().flatten() + self.merge.adds().flatten() } pub fn as_merge(&self) -> &Merge> { diff --git a/lib/src/simple_op_store.rs b/lib/src/simple_op_store.rs index f6c30bf5aa..6ca185fde5 100644 --- a/lib/src/simple_op_store.rs +++ b/lib/src/simple_op_store.rs @@ -444,8 +444,8 @@ fn ref_target_to_proto(value: &RefTarget) -> Option panic!("unexpected value"), }; @@ -136,12 +136,12 @@ fn test_same_type() { .read_conflict(&RepoPath::from_internal_string("a_b"), id) .unwrap(); assert_eq!( - conflict.removes(), - vec![base_tree.value(&component).cloned()] + conflict.removes().map(|v| v.as_ref()).collect_vec(), + vec![base_tree.value(&component)] ); assert_eq!( - conflict.adds(), - vec![side2_tree.value(&component).cloned(), None] + conflict.adds().map(|v| v.as_ref()).collect_vec(), + vec![side2_tree.value(&component), None] ); } _ => panic!("unexpected value"), @@ -153,12 +153,12 @@ fn test_same_type() { .read_conflict(&RepoPath::from_internal_string("ab_"), id) .unwrap(); assert_eq!( - conflict.removes(), - vec![base_tree.value(&component).cloned()] + conflict.removes().map(|v| v.as_ref()).collect_vec(), + vec![base_tree.value(&component)] ); assert_eq!( - conflict.adds(), - vec![side1_tree.value(&component).cloned(), None] + conflict.adds().map(|v| v.as_ref()).collect_vec(), + vec![side1_tree.value(&component), None] ); } _ => panic!("unexpected value"), @@ -170,15 +170,12 @@ fn test_same_type() { .read_conflict(&RepoPath::from_internal_string("abc"), id) .unwrap(); assert_eq!( - conflict.removes(), - vec![base_tree.value(&component).cloned()] + conflict.removes().map(|v| v.as_ref()).collect_vec(), + vec![base_tree.value(&component)] ); assert_eq!( - conflict.adds(), - vec![ - side1_tree.value(&component).cloned(), - side2_tree.value(&component).cloned(), - ] + conflict.adds().map(|v| v.as_ref()).collect_vec(), + vec![side1_tree.value(&component), side2_tree.value(&component)] ); } _ => panic!("unexpected value"), @@ -424,15 +421,12 @@ fn test_types() { ) .unwrap(); assert_eq!( - conflict.removes(), - vec![base_tree.value(&component).cloned()] + conflict.removes().map(|v| v.as_ref()).collect_vec(), + vec![base_tree.value(&component)] ); assert_eq!( - conflict.adds(), - vec![ - side1_tree.value(&component).cloned(), - side2_tree.value(&component).cloned(), - ] + conflict.adds().map(|v| v.as_ref()).collect_vec(), + vec![side1_tree.value(&component), side2_tree.value(&component)] ); } _ => panic!("unexpected value"), @@ -444,15 +438,12 @@ fn test_types() { .read_conflict(&RepoPath::from_internal_string("tree_normal_symlink"), id) .unwrap(); assert_eq!( - conflict.removes(), - vec![base_tree.value(&component).cloned()] + conflict.removes().map(|v| v.as_ref()).collect_vec(), + vec![base_tree.value(&component)] ); assert_eq!( - conflict.adds(), - vec![ - side1_tree.value(&component).cloned(), - side2_tree.value(&component).cloned(), - ] + conflict.adds().map(|v| v.as_ref()).collect_vec(), + vec![side1_tree.value(&component), side2_tree.value(&component)] ); } _ => panic!("unexpected value"), @@ -507,14 +498,14 @@ fn test_simplify_conflict() { .read_conflict(&RepoPath::from_components(vec![component.clone()]), id) .unwrap(); assert_eq!( - conflict.removes(), - vec![base_tree.value(&component).cloned()] + conflict.removes().map(|v| v.as_ref()).collect_vec(), + vec![base_tree.value(&component)] ); assert_eq!( - conflict.adds(), + conflict.adds().map(|v| v.as_ref()).collect_vec(), vec![ - branch_tree.value(&component).cloned(), - upstream2_tree.value(&component).cloned(), + branch_tree.value(&component), + upstream2_tree.value(&component), ] ); } @@ -526,14 +517,14 @@ fn test_simplify_conflict() { TreeValue::Conflict(id) => { let conflict = store.read_conflict(&path, id).unwrap(); assert_eq!( - conflict.removes(), - vec![base_tree.value(&component).cloned()] + conflict.removes().map(|v| v.as_ref()).collect_vec(), + vec![base_tree.value(&component)] ); assert_eq!( - conflict.adds(), + conflict.adds().map(|v| v.as_ref()).collect_vec(), vec![ - upstream2_tree.value(&component).cloned(), - branch_tree.value(&component).cloned(), + upstream2_tree.value(&component), + branch_tree.value(&component) ] ); }