Skip to content

Commit 9ddd0b9

Browse files
committed
refactor: Drop lazy_static dependency
Fixes #514
1 parent 6e4e022 commit 9ddd0b9

File tree

7 files changed

+40
-36
lines changed

7 files changed

+40
-36
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ preserve_order = ["indexmap", "toml?/preserve_order", "serde_json?/preserve_orde
121121
async = ["async-trait"]
122122

123123
[dependencies]
124-
lazy_static = "1.4"
125124
serde = "1.0"
126125
nom = "7"
127126

examples/global/main.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
#![allow(deprecated)]
22
use config::Config;
3-
use lazy_static::lazy_static;
43
use std::error::Error;
4+
use std::sync::OnceLock;
55
use std::sync::RwLock;
66

7-
lazy_static! {
8-
static ref SETTINGS: RwLock<Config> = RwLock::new(Config::default());
7+
fn settings() -> &'static RwLock<Config> {
8+
static CONFIG: OnceLock<RwLock<Config>> = OnceLock::new();
9+
CONFIG.get_or_init(|| RwLock::new(Config::default()))
910
}
1011

1112
fn try_main() -> Result<(), Box<dyn Error>> {
1213
// Set property
13-
SETTINGS.write()?.set("property", 42)?;
14+
settings().write()?.set("property", 42)?;
1415

1516
// Get property
16-
println!("property: {}", SETTINGS.read()?.get::<i32>("property")?);
17+
println!("property: {}", settings().read()?.get::<i32>("property")?);
1718

1819
Ok(())
1920
}

examples/static_env.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1+
use std::sync::OnceLock;
2+
13
use config::Config;
24

3-
lazy_static::lazy_static! {
4-
#[derive(Debug)]
5-
pub static ref CONFIG: Config = Config::builder()
6-
.add_source(config::Environment::with_prefix("APP_NAME").separator("_"))
7-
.build()
8-
.unwrap();
5+
fn config() -> &'static Config {
6+
static CONFIG: OnceLock<Config> = OnceLock::new();
7+
CONFIG.get_or_init(|| {
8+
Config::builder()
9+
.add_source(config::Environment::with_prefix("APP_NAME").separator("_"))
10+
.build()
11+
.unwrap()
12+
})
913
}
1014

1115
/// Get a configuration value from the static configuration object
1216
pub fn get<'a, T: serde::Deserialize<'a>>(key: &str) -> T {
1317
// You shouldn't probably do it like that and actually handle that error that might happen
1418
// here, but for the sake of simplicity, we do it like this here
15-
CONFIG.get::<T>(key).unwrap()
19+
config().get::<T>(key).unwrap()
1620
}
1721

1822
fn main() {

examples/watch/main.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,26 @@ use notify::{Event, RecommendedWatcher, RecursiveMode, Watcher};
44
use std::collections::HashMap;
55
use std::path::Path;
66
use std::sync::mpsc::channel;
7+
use std::sync::OnceLock;
78
use std::sync::RwLock;
89
use std::time::Duration;
910

10-
lazy_static::lazy_static! {
11-
static ref SETTINGS: RwLock<Config> = RwLock::new({
11+
fn settings() -> &'static RwLock<Config> {
12+
static CONFIG: OnceLock<RwLock<Config>> = OnceLock::new();
13+
CONFIG.get_or_init(|| {
1214
let mut settings = Config::default();
13-
settings.merge(File::with_name("examples/watch/Settings.toml")).unwrap();
14-
1515
settings
16-
});
16+
.merge(File::with_name("examples/watch/Settings.toml"))
17+
.unwrap();
18+
19+
RwLock::new(settings)
20+
})
1721
}
1822

1923
fn show() {
2024
println!(
2125
" * Settings :: \n\x1b[31m{:?}\x1b[0m",
22-
SETTINGS
26+
settings()
2327
.read()
2428
.unwrap()
2529
.clone()
@@ -58,7 +62,7 @@ fn watch() -> ! {
5862
..
5963
})) => {
6064
println!(" * Settings.toml written; refreshing configuration ...");
61-
SETTINGS.write().unwrap().refresh().unwrap();
65+
settings().write().unwrap().refresh().unwrap();
6266
show();
6367
}
6468

src/file/format/mod.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
// If no features are used, there is an "unused mut" warning in `ALL_EXTENSIONS`
2-
// BUG: ? For some reason this doesn't do anything if I try and function scope this
3-
#![allow(unused_mut)]
4-
5-
use lazy_static::lazy_static;
61
use std::collections::HashMap;
72
use std::error::Error;
3+
use std::sync::OnceLock;
84

95
use crate::map::Map;
106
use crate::{file::FileStoredFormat, value::Value, Format};
@@ -57,10 +53,11 @@ pub enum FileFormat {
5753
Json5,
5854
}
5955

60-
lazy_static! {
61-
#[doc(hidden)]
62-
// #[allow(unused_mut)] ?
63-
pub static ref ALL_EXTENSIONS: HashMap<FileFormat, Vec<&'static str>> = {
56+
pub(crate) fn all_extensions() -> &'static HashMap<FileFormat, Vec<&'static str>> {
57+
#![allow(unused_mut)] // If no features are used, there is an "unused mut" warning in `all_extensions`
58+
59+
static ALL_EXTENSIONS: OnceLock<HashMap<FileFormat, Vec<&'static str>>> = OnceLock::new();
60+
ALL_EXTENSIONS.get_or_init(|| {
6461
let mut formats: HashMap<FileFormat, Vec<_>> = HashMap::new();
6562

6663
#[cfg(feature = "toml")]
@@ -82,15 +79,15 @@ lazy_static! {
8279
formats.insert(FileFormat::Json5, vec!["json5"]);
8380

8481
formats
85-
};
82+
})
8683
}
8784

8885
impl FileFormat {
8986
pub(crate) fn extensions(&self) -> &'static [&'static str] {
9087
// It should not be possible for this to fail
9188
// A FileFormat would need to be declared without being added to the
92-
// ALL_EXTENSIONS map.
93-
ALL_EXTENSIONS.get(self).unwrap()
89+
// all_extensions map.
90+
all_extensions().get(self).unwrap()
9491
}
9592

9693
pub(crate) fn parse(

src/file/source/file.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::io;
55
use std::path::PathBuf;
66

77
use crate::file::{
8-
format::ALL_EXTENSIONS, source::FileSourceResult, FileSource, FileStoredFormat, Format,
8+
format::all_extensions, source::FileSourceResult, FileSource, FileStoredFormat, Format,
99
};
1010

1111
/// Describes a file sourced from a file
@@ -38,7 +38,7 @@ impl FileSourceFile {
3838
return if let Some(format) = format_hint {
3939
Ok((filename, Box::new(format)))
4040
} else {
41-
for (format, extensions) in ALL_EXTENSIONS.iter() {
41+
for (format, extensions) in all_extensions().iter() {
4242
if extensions.contains(
4343
&filename
4444
.extension()
@@ -75,7 +75,7 @@ impl FileSourceFile {
7575
}
7676

7777
None => {
78-
for format in ALL_EXTENSIONS.keys() {
78+
for format in all_extensions().keys() {
7979
for ext in format.extensions() {
8080
filename.set_extension(ext);
8181

0 commit comments

Comments
 (0)