Skip to content

Commit

Permalink
use mutable references instead of owned values in builder methods
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Nov 17, 2023
1 parent 0ef6e53 commit 7b4bbcf
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 86 deletions.
11 changes: 8 additions & 3 deletions benches/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ pub trait Metric {
type Parser: Parser<Self>;

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<M: Metric + ?Sized>: Default {
Expand Down Expand Up @@ -64,7 +69,7 @@ fn for_all_cases_and_ranges<M, F>(
CaseSensitivity::Smart,
] {
for with_ranges in [true, false] {
metric = metric
metric
.with_case_sensitivity(case)
.with_matched_ranges(with_ranges);

Expand Down
7 changes: 5 additions & 2 deletions benches/fzf_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
7 changes: 5 additions & 2 deletions benches/fzf_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
14 changes: 8 additions & 6 deletions fuzz/fuzz_targets/fzf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()];
}
Expand Down
10 changes: 5 additions & 5 deletions src/algos/fzf/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
14 changes: 7 additions & 7 deletions src/algos/fzf/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
24 changes: 12 additions & 12 deletions tests/fzf_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,24 +561,24 @@ mod utils {
+ for<'a> Metric<Query<'a> = 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)
}

Expand All @@ -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)
}

Expand All @@ -625,9 +625,9 @@ mod utils {
query: &str,
candidate: &str,
) -> (F, Option<Match<FzfDistance>>) {
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();

Expand Down
54 changes: 33 additions & 21 deletions tests/fzf_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,70 +218,82 @@ 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();

let query = parser.parse("z\n");

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]);
}
Loading

0 comments on commit 7b4bbcf

Please sign in to comment.