matcher.rs

 1use std::sync::Mutex;
 2
 3static MATCHERS: Mutex<Vec<nucleo::Matcher>> = Mutex::new(Vec::new());
 4
 5pub const LENGTH_PENALTY: f64 = 0.01;
 6
 7pub fn get_matcher(config: nucleo::Config) -> nucleo::Matcher {
 8    let mut matchers = MATCHERS.lock().unwrap();
 9    match matchers.pop() {
10        Some(mut matcher) => {
11            matcher.config = config;
12            matcher
13        }
14        None => nucleo::Matcher::new(config),
15    }
16}
17
18pub fn return_matcher(matcher: nucleo::Matcher) {
19    MATCHERS.lock().unwrap().push(matcher);
20}
21
22pub fn get_matchers(n: usize, config: nucleo::Config) -> Vec<nucleo::Matcher> {
23    let mut matchers: Vec<_> = {
24        let mut pool = MATCHERS.lock().unwrap();
25        let available = pool.len().min(n);
26        pool.drain(..available)
27            .map(|mut matcher| {
28                matcher.config = config.clone();
29                matcher
30            })
31            .collect()
32    };
33    matchers.resize_with(n, || nucleo::Matcher::new(config.clone()));
34    matchers
35}
36
37pub fn return_matchers(mut matchers: Vec<nucleo::Matcher>) {
38    MATCHERS.lock().unwrap().append(&mut matchers);
39}