diff --git a/Cargo.lock b/Cargo.lock index d367d2b..ebc63ea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -341,7 +341,6 @@ dependencies = [ "criterion", "fuzzy-matcher", "ignore", - "lazy_static", "rayon", ] diff --git a/Cargo.toml b/Cargo.toml index 1350380..f5164a3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,6 @@ path = "rust/lib.rs" [dependencies] ignore = "0.4.20" fuzzy-matcher = "0.3.7" -lazy_static = "1.4.0" rayon = "1.7.0" [dev-dependencies] diff --git a/rust/lib.rs b/rust/lib.rs index 3a0848d..6d9ba73 100644 --- a/rust/lib.rs +++ b/rust/lib.rs @@ -7,12 +7,24 @@ use std::ffi::CStr; use std::ffi::CString; use std::os::raw::{c_char, c_int}; use std::sync::Mutex; +use std::sync::OnceLock; -#[macro_use] -extern crate lazy_static; +struct Ivy { + pub file_cache: HashMap>, +} -lazy_static! { - static ref GLOBAL_FILE_CACHE: Mutex>> = Mutex::new(HashMap::new()); +static INSTANCE: OnceLock> = OnceLock::new(); + +impl Ivy { + pub fn new() -> Self { + Self { + file_cache: HashMap::new(), + } + } + + pub fn global() -> &'static Mutex { + INSTANCE.get_or_init(|| Mutex::new(Ivy::new())) + } } fn to_string(input: *const c_char) -> String { @@ -23,15 +35,17 @@ fn to_string(input: *const c_char) -> String { } fn get_files(directory: &String) -> Vec { - let mut cache = GLOBAL_FILE_CACHE.lock().unwrap(); - if !cache.contains_key(directory) { + let mut ivy = Ivy::global().lock().unwrap(); + if !ivy.file_cache.contains_key(directory) { let finder_options = finder::Options { directory: directory.clone(), }; - cache.insert(directory.clone(), finder::find_files(finder_options)); + + ivy.file_cache + .insert(directory.clone(), finder::find_files(finder_options)); } - return cache.get(directory).unwrap().to_vec(); + return ivy.file_cache.get(directory).unwrap().to_vec(); } #[no_mangle]