From 24c0190f743647dfa0138b078ccbd7dd6071cd94 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Thu, 29 Jun 2023 10:47:44 -0700 Subject: [PATCH] conflicts: rename `try_map()` to `maybe_map()` I'm going to add a `Result` version and it makes more sense to call that `try_map()`. --- lib/src/conflicts.rs | 21 ++++++++++++--------- lib/src/tree.rs | 4 ++-- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/src/conflicts.rs b/lib/src/conflicts.rs index 5fba0a471..95951bf12 100644 --- a/lib/src/conflicts.rs +++ b/lib/src/conflicts.rs @@ -126,12 +126,15 @@ impl Conflict { /// Creates a new conflict by applying `f` to each remove and add. pub fn map<'a, U>(&'a self, mut f: impl FnMut(&'a T) -> U) -> Conflict { - self.try_map(|term| Some(f(term))).unwrap() + self.maybe_map(|term| Some(f(term))).unwrap() } /// Creates a new conflict by applying `f` to each remove and add, returning /// `None if `f` returns `None` for any of them. - pub fn try_map<'a, U>(&'a self, mut f: impl FnMut(&'a T) -> Option) -> Option> { + pub fn maybe_map<'a, U>( + &'a self, + mut f: impl FnMut(&'a T) -> Option, + ) -> Option> { let removes = self.removes.iter().map(&mut f).collect::>()?; let adds = self.adds.iter().map(&mut f).collect::>()?; Some(Conflict { removes, adds }) @@ -196,7 +199,7 @@ impl Conflict> { } pub fn to_file_conflict(&self) -> Option>> { - self.try_map(|term| match term { + self.maybe_map(|term| match term { None => Some(None), Some(TreeValue::File { id, @@ -766,7 +769,7 @@ mod tests { } #[test] - fn test_try_map() { + fn test_maybe_map() { fn sqrt(i: &i32) -> Option { if *i >= 0 { Some((*i as f64).sqrt() as i32) @@ -778,11 +781,11 @@ mod tests { Conflict::new(removes.to_vec(), adds.to_vec()) } // 1-way conflict - assert_eq!(c(&[], &[1]).try_map(sqrt), Some(c(&[], &[1]))); - assert_eq!(c(&[], &[-1]).try_map(sqrt), None); + assert_eq!(c(&[], &[1]).maybe_map(sqrt), Some(c(&[], &[1]))); + assert_eq!(c(&[], &[-1]).maybe_map(sqrt), None); // 3-way conflict - assert_eq!(c(&[1], &[4, 9]).try_map(sqrt), Some(c(&[1], &[2, 3]))); - assert_eq!(c(&[-1], &[4, 9]).try_map(sqrt), None); - assert_eq!(c(&[1], &[-4, 9]).try_map(sqrt), None); + assert_eq!(c(&[1], &[4, 9]).maybe_map(sqrt), Some(c(&[1], &[2, 3]))); + assert_eq!(c(&[-1], &[4, 9]).maybe_map(sqrt), None); + assert_eq!(c(&[1], &[-4, 9]).maybe_map(sqrt), None); } } diff --git a/lib/src/tree.rs b/lib/src/tree.rs index 207774eee..6c72c1e36 100644 --- a/lib/src/tree.rs +++ b/lib/src/tree.rs @@ -648,7 +648,7 @@ fn try_resolve_file_conflict( // we can't merge them anyway. At the same time we determine whether the // resulting file should be executable. // TODO: Change to let-else once our MSRV is above 1.65 - let file_id_conflict = if let Some(conflict) = conflict.try_map(|term| match term { + let file_id_conflict = if let Some(conflict) = conflict.maybe_map(|term| match term { Some(TreeValue::File { id, executable: _ }) => Some(id), _ => None, }) { @@ -657,7 +657,7 @@ fn try_resolve_file_conflict( return Ok(None); }; // TODO: Change to let-else once our MSRV is above 1.65 - let executable_conflict = if let Some(conflict) = conflict.try_map(|term| match term { + let executable_conflict = if let Some(conflict) = conflict.maybe_map(|term| match term { Some(TreeValue::File { id: _, executable }) => Some(executable), _ => None, }) {