diff --git a/Cargo.lock b/Cargo.lock index a66ee181..44022d4e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -154,7 +154,6 @@ dependencies = [ "glob", "indexmap 2.0.2", "json5", - "lazy_static", "log", "nom", "notify", diff --git a/Cargo.toml b/Cargo.toml index e5f5950f..8974bc94 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,6 @@ preserve_order = ["indexmap", "toml?/preserve_order", "serde_json?/preserve_orde async = ["async-trait"] [dependencies] -lazy_static = "1.4" serde = "1.0" nom = "7" diff --git a/src/file/format/mod.rs b/src/file/format/mod.rs index 025e98a9..2b7e8932 100644 --- a/src/file/format/mod.rs +++ b/src/file/format/mod.rs @@ -1,10 +1,6 @@ -// If no features are used, there is an "unused mut" warning in `ALL_EXTENSIONS` -// BUG: ? For some reason this doesn't do anything if I try and function scope this -#![allow(unused_mut)] - -use lazy_static::lazy_static; use std::collections::HashMap; use std::error::Error; +use std::sync::OnceLock; use crate::map::Map; use crate::{file::FileStoredFormat, value::Value, Format}; @@ -57,10 +53,11 @@ pub enum FileFormat { Json5, } -lazy_static! { - #[doc(hidden)] - // #[allow(unused_mut)] ? - pub static ref ALL_EXTENSIONS: HashMap> = { +pub(crate) fn all_extensions() -> &'static HashMap> { + #![allow(unused_mut)] // If no features are used, there is an "unused mut" warning in `all_extensions` + + static ALL_EXTENSIONS: OnceLock>> = OnceLock::new(); + ALL_EXTENSIONS.get_or_init(|| { let mut formats: HashMap> = HashMap::new(); #[cfg(feature = "toml")] @@ -82,15 +79,15 @@ lazy_static! { formats.insert(FileFormat::Json5, vec!["json5"]); formats - }; + }) } impl FileFormat { pub(crate) fn extensions(&self) -> &'static [&'static str] { // It should not be possible for this to fail // A FileFormat would need to be declared without being added to the - // ALL_EXTENSIONS map. - ALL_EXTENSIONS.get(self).unwrap() + // all_extensions map. + all_extensions().get(self).unwrap() } pub(crate) fn parse( diff --git a/src/file/source/file.rs b/src/file/source/file.rs index 8ee5d315..0e0687c2 100644 --- a/src/file/source/file.rs +++ b/src/file/source/file.rs @@ -5,7 +5,7 @@ use std::io; use std::path::PathBuf; use crate::file::{ - format::ALL_EXTENSIONS, source::FileSourceResult, FileSource, FileStoredFormat, Format, + format::all_extensions, source::FileSourceResult, FileSource, FileStoredFormat, Format, }; /// Describes a file sourced from a file @@ -38,7 +38,7 @@ impl FileSourceFile { return if let Some(format) = format_hint { Ok((filename, Box::new(format))) } else { - for (format, extensions) in ALL_EXTENSIONS.iter() { + for (format, extensions) in all_extensions().iter() { if extensions.contains( &filename .extension() @@ -75,7 +75,7 @@ impl FileSourceFile { } None => { - for format in ALL_EXTENSIONS.keys() { + for format in all_extensions().keys() { for ext in format.extensions() { filename.set_extension(ext);