From c2e6738f6b4dec16d26f23c26a1e5666de12ce99 Mon Sep 17 00:00:00 2001 From: Lynnesbian Date: Fri, 24 Sep 2021 23:53:02 +1000 Subject: [PATCH] use Lazy instead of OnceCell for MIMEDB --- src/files.rs | 3 ++- src/lib.rs | 27 +++++---------------------- src/main.rs | 4 +--- src/tests/mod.rs | 3 --- 4 files changed, 8 insertions(+), 29 deletions(-) diff --git a/src/files.rs b/src/files.rs index d4c13a4..2c6cdde 100644 --- a/src/files.rs +++ b/src/files.rs @@ -2,6 +2,7 @@ use std::collections::{BTreeSet, HashMap}; use std::fs::File; use std::io; use std::io::{Read, Seek, SeekFrom}; +use std::ops::Deref; use std::path::Path; use std::str::FromStr; use std::sync::RwLock; @@ -96,7 +97,7 @@ pub fn wanted_file( pub fn scan_file(entry: &DirEntry, canonical_paths: bool) -> Result { let path = entry.path(); // try to determine mimetype for this entry - let result = match mime_type(MIMEDB.get().unwrap(), path) { + let result = match mime_type(MIMEDB.deref(), path) { // an error occurred while trying to read the file Err(_) => return Err(ScanError::File(path)), // the file was read successfully, but we were unable to determine its mimetype diff --git a/src/lib.rs b/src/lib.rs index 2e81396..2e27eb8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,7 +9,7 @@ pub mod parameters; pub mod utils; use cfg_if::cfg_if; -use once_cell::sync::OnceCell; +use once_cell::sync::Lazy; use crate::findings::Findings; use crate::mime_db::MimeDb; @@ -26,27 +26,10 @@ cfg_if! { cfg_if! { if #[cfg(any(all(unix, feature = "infer-backend"), all(not(unix), not(feature = "xdg-mime-backend"))))] { - /// A [OnceCell] holding an instance of [mime_db::MimeDb]. - pub static MIMEDB: OnceCell = OnceCell::new(); + /// A [Lazy] holding an instance of [mime_db::MimeDb]. + pub static MIMEDB: Lazy = Lazy::new(|| crate::mime_db::InferDb::init()); } else { - /// A [OnceCell] holding an instance of [mime_db::MimeDb]. - pub static MIMEDB: OnceCell = OnceCell::new(); - } -} - -/// Initialises [`MIMEDB`] with a value dependent on the current backend. -pub fn init_db() { - cfg_if! { - if #[cfg(any(all(unix, feature = "infer-backend"), all(not(unix), not(feature = "xdg-mime-backend"))))] { - MIMEDB - .set(crate::mime_db::InferDb::init()) - .or(Err("Failed to initialise Infer backend!")) - .unwrap(); - } else { - MIMEDB - .set(crate::mime_db::XdgDb::init()) - .or(Err("Failed to initialise XDG Mime backend!")) - .unwrap(); - } + /// A [Lazy] holding an instance of [mime_db::MimeDb]. + pub static MIMEDB: Lazy = Lazy::new(|| crate::mime_db::XdgDb::init()); } } diff --git a/src/main.rs b/src/main.rs index ba53f94..a044c62 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,7 +25,7 @@ use fif::files::{scan_directory, scan_from_walkdir}; use fif::formats::Format; use fif::parameters::OutputFormat; use fif::utils::{clap_long_version, os_name}; -use fif::{formats, init_db, parameters}; +use fif::{formats, parameters}; use log::{debug, error, info, trace, warn, Level}; #[cfg(test)] @@ -58,8 +58,6 @@ fn main() { std::env::consts::ARCH, os_name() ); - trace!("Initialising mimetype database"); - init_db(); debug!("Iterating directory: {:?}", args.dir); diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 4160a44..5d23395 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -128,9 +128,6 @@ fn simple_directory() { // there should be one file missing: "ignore.fake_ext" assert_eq!(entries.len(), files.len() - 1); - // initialise global mime DB - this is needed because `scan_from_walkdir` expects it to be present. - crate::init_db(); - let results = scan_from_walkdir(&entries, false); let canonical_results = scan_from_walkdir(&entries, true); assert_eq!(results.len(), canonical_results.len());