matcher.rs

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