From 7b4bbcf7c123ee8e3b5d8f4ec6fc206c7d797227 Mon Sep 17 00:00:00 2001 From: Riccardo Mazzarini Date: Fri, 17 Nov 2023 10:55:12 +0100 Subject: [PATCH] use mutable references instead of owned values in builder methods --- benches/common.rs | 11 +++++-- benches/fzf_v1.rs | 7 +++-- benches/fzf_v2.rs | 7 +++-- fuzz/fuzz_targets/fzf.rs | 14 +++++---- src/algos/fzf/v1.rs | 10 +++--- src/algos/fzf/v2.rs | 14 ++++----- tests/fzf_common.rs | 24 +++++++------- tests/fzf_v1.rs | 54 +++++++++++++++++++------------- tests/fzf_v2.rs | 67 +++++++++++++++++++++++----------------- 9 files changed, 122 insertions(+), 86 deletions(-) diff --git a/benches/common.rs b/benches/common.rs index 427f926..60fe187 100644 --- a/benches/common.rs +++ b/benches/common.rs @@ -12,8 +12,13 @@ pub trait Metric { type Parser: Parser; fn dist(&mut self, query: Self::Query<'_>, candidate: &str); - fn with_case_sensitivity(self, case_sensitivity: CaseSensitivity) -> Self; - fn with_matched_ranges(self, matched_ranges: bool) -> Self; + + fn with_case_sensitivity( + &mut self, + case_sensitivity: CaseSensitivity, + ) -> &mut Self; + + fn with_matched_ranges(&mut self, matched_ranges: bool) -> &mut Self; } pub trait Parser: Default { @@ -64,7 +69,7 @@ fn for_all_cases_and_ranges( CaseSensitivity::Smart, ] { for with_ranges in [true, false] { - metric = metric + metric .with_case_sensitivity(case) .with_matched_ranges(with_ranges); diff --git a/benches/fzf_v1.rs b/benches/fzf_v1.rs index 5133c7e..2a57ab2 100644 --- a/benches/fzf_v1.rs +++ b/benches/fzf_v1.rs @@ -29,10 +29,13 @@ impl bench::Metric for FzfV1 { fn dist(&mut self, query: FzfQuery, candidate: &str) { self.distance(query, candidate); } - fn with_case_sensitivity(self, case_sensitivity: CaseSensitivity) -> Self { + fn with_case_sensitivity( + &mut self, + case_sensitivity: CaseSensitivity, + ) -> &mut Self { self.with_case_sensitivity(case_sensitivity) } - fn with_matched_ranges(self, matched_ranges: bool) -> Self { + fn with_matched_ranges(&mut self, matched_ranges: bool) -> &mut Self { self.with_matched_ranges(matched_ranges) } } diff --git a/benches/fzf_v2.rs b/benches/fzf_v2.rs index 14de163..07aa2bf 100644 --- a/benches/fzf_v2.rs +++ b/benches/fzf_v2.rs @@ -29,10 +29,13 @@ impl bench::Metric for FzfV2 { fn dist(&mut self, query: FzfQuery, candidate: &str) { self.distance(query, candidate); } - fn with_case_sensitivity(self, case_sensitivity: CaseSensitivity) -> Self { + fn with_case_sensitivity( + &mut self, + case_sensitivity: CaseSensitivity, + ) -> &mut Self { self.with_case_sensitivity(case_sensitivity) } - fn with_matched_ranges(self, matched_ranges: bool) -> Self { + fn with_matched_ranges(&mut self, matched_ranges: bool) -> &mut Self { self.with_matched_ranges(matched_ranges) } } diff --git a/fuzz/fuzz_targets/fzf.rs b/fuzz/fuzz_targets/fzf.rs index 666fbf1..cc4c2aa 100644 --- a/fuzz/fuzz_targets/fzf.rs +++ b/fuzz/fuzz_targets/fzf.rs @@ -42,23 +42,25 @@ fuzz_target!(|data: (Query, Candidate)| { let mut fzf_v2 = FzfV2::new(); with_opts(|case_sensitivity, normalization, scheme| { - fzf_v1 = core::mem::take(&mut fzf_v1) + let mach = fzf_v1 .with_case_sensitivity(case_sensitivity) .with_normalization(normalization) - .with_scoring_scheme(scheme); + .with_scoring_scheme(scheme) + .distance(query, candidate); - if let Some(mach) = fzf_v1.distance(query, candidate) { + if let Some(mach) = mach { for range in mach.matched_ranges() { let _s = &candidate[range.clone()]; } } - fzf_v2 = core::mem::take(&mut fzf_v2) + let mach = fzf_v2 .with_case_sensitivity(case_sensitivity) .with_normalization(normalization) - .with_scoring_scheme(scheme); + .with_scoring_scheme(scheme) + .distance(query, candidate); - if let Some(mach) = fzf_v2.distance(query, candidate) { + if let Some(mach) = mach { for range in mach.matched_ranges() { let _s = &candidate[range.clone()]; } diff --git a/src/algos/fzf/v1.rs b/src/algos/fzf/v1.rs index b142a33..f5508b5 100644 --- a/src/algos/fzf/v1.rs +++ b/src/algos/fzf/v1.rs @@ -48,30 +48,30 @@ impl FzfV1 { /// TODO: docs #[inline] pub fn with_case_sensitivity( - mut self, + &mut self, case_sensitivity: CaseSensitivity, - ) -> Self { + ) -> &mut Self { self.case_sensitivity = case_sensitivity; self } /// TODO: docs #[inline] - pub fn with_matched_ranges(mut self, matched_ranges: bool) -> Self { + pub fn with_matched_ranges(&mut self, matched_ranges: bool) -> &mut Self { self.with_matched_ranges = matched_ranges; self } /// TODO: docs #[inline] - pub fn with_normalization(mut self, normalization: bool) -> Self { + pub fn with_normalization(&mut self, normalization: bool) -> &mut Self { self.normalization = normalization; self } /// TODO: docs #[inline] - pub fn with_scoring_scheme(mut self, scheme: FzfScheme) -> Self { + pub fn with_scoring_scheme(&mut self, scheme: FzfScheme) -> &mut Self { self.scheme = scheme.into_inner(); self } diff --git a/src/algos/fzf/v2.rs b/src/algos/fzf/v2.rs index bda7654..61b7192 100644 --- a/src/algos/fzf/v2.rs +++ b/src/algos/fzf/v2.rs @@ -49,30 +49,30 @@ impl FzfV2 { /// TODO: docs #[inline] pub fn with_case_sensitivity( - mut self, + &mut self, case_sensitivity: CaseSensitivity, - ) -> Self { + ) -> &mut Self { self.case_sensitivity = case_sensitivity; self } /// TODO: docs #[inline] - pub fn with_normalization(mut self, normalization: bool) -> Self { - self.normalization = normalization; + pub fn with_matched_ranges(&mut self, matched_ranges: bool) -> &mut Self { + self.with_matched_ranges = matched_ranges; self } /// TODO: docs #[inline] - pub fn with_matched_ranges(mut self, matched_ranges: bool) -> Self { - self.with_matched_ranges = matched_ranges; + pub fn with_normalization(&mut self, normalization: bool) -> &mut Self { + self.normalization = normalization; self } /// TODO: docs #[inline] - pub fn with_scoring_scheme(mut self, scheme: FzfScheme) -> Self { + pub fn with_scoring_scheme(&mut self, scheme: FzfScheme) -> &mut Self { self.scheme = scheme.into_inner(); self } diff --git a/tests/fzf_common.rs b/tests/fzf_common.rs index ddba1b3..653e835 100644 --- a/tests/fzf_common.rs +++ b/tests/fzf_common.rs @@ -561,24 +561,24 @@ mod utils { + for<'a> Metric = FzfQuery<'a>, Distance = FzfDistance> { fn with_case_sensitivity( - self, + &mut self, case_sensitivity: CaseSensitivity, - ) -> Self; + ) -> &mut Self; - fn with_matched_ranges(self, matched_ranges: bool) -> Self; + fn with_matched_ranges(&mut self, matched_ranges: bool) -> &mut Self; fn scheme(&self) -> &norm::fzf::Scheme; } impl Fzf for FzfV1 { fn with_case_sensitivity( - self, + &mut self, case_sensitivity: CaseSensitivity, - ) -> Self { + ) -> &mut Self { self.with_case_sensitivity(case_sensitivity) } - fn with_matched_ranges(self, matched_ranges: bool) -> Self { + fn with_matched_ranges(&mut self, matched_ranges: bool) -> &mut Self { self.with_matched_ranges(matched_ranges) } @@ -597,13 +597,13 @@ mod utils { impl Fzf for FzfV2 { fn with_case_sensitivity( - self, + &mut self, case_sensitivity: CaseSensitivity, - ) -> Self { + ) -> &mut Self { self.with_case_sensitivity(case_sensitivity) } - fn with_matched_ranges(self, matched_ranges: bool) -> Self { + fn with_matched_ranges(&mut self, matched_ranges: bool) -> &mut Self { self.with_matched_ranges(matched_ranges) } @@ -625,9 +625,9 @@ mod utils { query: &str, candidate: &str, ) -> (F, Option>) { - let mut fzf = F::default() - .with_case_sensitivity(case_sensitivity) - .with_matched_ranges(true); + let mut fzf = F::default(); + + fzf.with_case_sensitivity(case_sensitivity).with_matched_ranges(true); let mut parser = FzfParser::new(); diff --git a/tests/fzf_v1.rs b/tests/fzf_v1.rs index 1fe2c91..ebfde0b 100644 --- a/tests/fzf_v1.rs +++ b/tests/fzf_v1.rs @@ -218,48 +218,54 @@ fn fzf_v1_upstream_suffix_6() { #[test] fn fzf_v1_score_1() { - let mut fzf = FzfV1::new() - .with_case_sensitivity(CaseSensitivity::Sensitive) - .with_matched_ranges(true); + let mut fzf = FzfV1::new(); let mut parser = FzfParser::new(); - let mach = fzf.distance(parser.parse("ZZ"), "ӥZZZ").unwrap(); + let mach = fzf + .with_case_sensitivity(CaseSensitivity::Sensitive) + .with_matched_ranges(true) + .distance(parser.parse("ZZ"), "ӥZZZ") + .unwrap(); assert_eq!(mach.matched_ranges(), [2..4]); } #[test] fn fzf_v1_score_2() { - let mut fzf = FzfV1::new() - .with_case_sensitivity(CaseSensitivity::Sensitive) - .with_matched_ranges(true); + let mut fzf = FzfV1::new(); let mut parser = FzfParser::new(); let query = parser.parse("^\\$ ]]%]]'\0\0\0\0\0\0"); - assert!(fzf.distance(query, "\0").is_none()); + let mach = fzf + .with_case_sensitivity(CaseSensitivity::Sensitive) + .with_matched_ranges(true) + .distance(query, "\0"); + + assert!(mach.is_none()); } #[test] fn fzf_v1_score_3() { - let mut fzf = FzfV1::new() - .with_case_sensitivity(CaseSensitivity::Sensitive) - .with_matched_ranges(true); + let mut fzf = FzfV1::new(); let mut parser = FzfParser::new(); let query = parser.parse("^\\$"); - assert!(fzf.distance(query, " ").is_none()); + let mach = fzf + .with_case_sensitivity(CaseSensitivity::Sensitive) + .with_matched_ranges(true) + .distance(query, " "); + + assert!(mach.is_none()); } #[test] fn fzf_v1_score_4() { - let mut fzf = FzfV1::new() - .with_case_sensitivity(CaseSensitivity::Insensitive) - .with_matched_ranges(true); + let mut fzf = FzfV1::new(); let mut parser = FzfParser::new(); @@ -267,21 +273,27 @@ fn fzf_v1_score_4() { let candidate = "ZZ\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\u{65e}\nZ\u{65e}"; - let mach = fzf.distance(query, candidate).unwrap(); + let mach = fzf + .with_case_sensitivity(CaseSensitivity::Insensitive) + .with_matched_ranges(true) + .distance(query, candidate) + .unwrap(); assert_eq!(mach.matched_ranges(), [1..2, 21..22]); } #[test] fn fzf_v1_score_5() { - let mut fzf = FzfV1::new() - .with_case_sensitivity(CaseSensitivity::Sensitive) - .with_matched_ranges(true) - .with_normalization(true); + let mut fzf = FzfV1::new(); let mut parser = FzfParser::new(); - let mach = fzf.distance(parser.parse("e !"), " !I\\hh+\u{364}").unwrap(); + let mach = fzf + .with_case_sensitivity(CaseSensitivity::Sensitive) + .with_matched_ranges(true) + .with_normalization(true) + .distance(parser.parse("e !"), " !I\\hh+\u{364}") + .unwrap(); assert_eq!(mach.matched_ranges(), [1..2, 7..9]); } diff --git a/tests/fzf_v2.rs b/tests/fzf_v2.rs index e35ed4c..7074eb0 100644 --- a/tests/fzf_v2.rs +++ b/tests/fzf_v2.rs @@ -219,13 +219,15 @@ fn fzf_v2_upstream_suffix_6() { #[test] fn fzf_v2_score_1() { - let mut fzf = FzfV2::new() - .with_case_sensitivity(CaseSensitivity::Sensitive) - .with_matched_ranges(true); + let mut fzf = FzfV2::new(); let mut parser = FzfParser::new(); - let mach = fzf.distance(parser.parse("jelly"), "jellyfish").unwrap(); + let mach = fzf + .with_case_sensitivity(CaseSensitivity::Sensitive) + .with_matched_ranges(true) + .distance(parser.parse("jelly"), "jellyfish") + .unwrap(); assert_eq!( mach.distance().into_score(), @@ -240,69 +242,78 @@ fn fzf_v2_score_1() { #[test] fn fzf_v2_score_2() { - let mut fzf = FzfV2::new() - .with_case_sensitivity(CaseSensitivity::Sensitive) - .with_matched_ranges(true); + let mut fzf = FzfV2::new(); let mut parser = FzfParser::new(); - let mach = fzf.distance(parser.parse("!$"), "$$2"); + let mach = fzf + .with_case_sensitivity(CaseSensitivity::Sensitive) + .with_matched_ranges(true) + .distance(parser.parse("!$"), "$$2"); assert!(mach.is_none()); } #[test] fn fzf_v2_score_3() { - let mut fzf = FzfV2::new() - .with_case_sensitivity(CaseSensitivity::Sensitive) - .with_matched_ranges(true); + let mut fzf = FzfV2::new(); let mut parser = FzfParser::new(); - let mach = - fzf.distance(parser.parse("\0\0"), "\0#B\0\u{364}\0\0").unwrap(); + let mach = fzf + .with_case_sensitivity(CaseSensitivity::Sensitive) + .with_matched_ranges(true) + .distance(parser.parse("\0\0"), "\0#B\0\u{364}\0\0") + .unwrap(); assert_eq!(mach.matched_ranges().sorted(), [6..8]); } #[test] fn fzf_v2_score_4() { - let mut fzf = FzfV2::new() - .with_case_sensitivity(CaseSensitivity::Sensitive) - .with_matched_ranges(true) - .with_normalization(true); + let mut fzf = FzfV2::new(); let mut parser = FzfParser::new(); - let mach = fzf.distance(parser.parse("e !"), " !I\\hh+\u{364}").unwrap(); + let mach = fzf + .with_case_sensitivity(CaseSensitivity::Sensitive) + .with_matched_ranges(true) + .with_normalization(true) + .distance(parser.parse("e !"), " !I\\hh+\u{364}") + .unwrap(); assert_eq!(mach.matched_ranges(), [1..2, 7..9]); } #[test] fn fzf_v2_score_5() { - let mut fzf = FzfV2::new() - .with_case_sensitivity(CaseSensitivity::Insensitive) - .with_matched_ranges(true) - .with_normalization(true); + let mut fzf = FzfV2::new(); let mut parser = FzfParser::new(); - let mach = fzf.distance(parser.parse("E"), "\u{364}E").unwrap(); + let mach = fzf + .with_case_sensitivity(CaseSensitivity::Insensitive) + .with_matched_ranges(true) + .with_normalization(true) + .distance(parser.parse("E"), "\u{364}E") + .unwrap(); assert_eq!(mach.matched_ranges(), [0..2]); } #[test] fn fzf_v2_score_6() { - let mut fzf = FzfV2::new() - .with_case_sensitivity(CaseSensitivity::Insensitive) - .with_matched_ranges(true) - .with_normalization(true); + let mut fzf = FzfV2::new(); let mut parser = FzfParser::new(); let query = parser.parse("!2\t\0\0\0WWHHWHWWWWWWWZ !I"); - assert!(fzf.distance(query, "\u{6}\0\0 N\u{364}\u{e}\u{365}+").is_none()); + let mach = fzf + .with_case_sensitivity(CaseSensitivity::Insensitive) + .with_matched_ranges(true) + .with_normalization(true) + .distance(query, "\u{6}\0\0 N\u{364}\u{e}\u{365}+"); + + assert!(mach.is_none()); }