Skip to content

Commit

Permalink
chore: use LazyLock
Browse files Browse the repository at this point in the history
Rust 1.80+
  • Loading branch information
LDeakin committed Jan 12, 2025
1 parent 6850c05 commit 7c38c8d
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions zarrs/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use crate::metadata::v3::array::codec;
use std::{
collections::HashMap,
sync::{OnceLock, RwLock, RwLockReadGuard, RwLockWriteGuard},
sync::{LazyLock, RwLock, RwLockReadGuard, RwLockWriteGuard},
};

#[cfg(doc)]
Expand Down Expand Up @@ -282,28 +282,22 @@ impl Config {
}
}

static CONFIG: OnceLock<RwLock<Config>> = OnceLock::new();
static CONFIG: LazyLock<RwLock<Config>> = LazyLock::new(|| RwLock::new(Config::default()));

/// Returns a reference to the global `zarrs` configuration.
///
/// # Panics
/// This function panics if the underlying lock has been poisoned and might panic if the global config is already held by the current thread.
pub fn global_config() -> RwLockReadGuard<'static, Config> {
CONFIG
.get_or_init(|| RwLock::new(Config::default()))
.read()
.unwrap()
CONFIG.read().unwrap()
}

/// Returns a mutable reference to the global `zarrs` configuration.
///
/// # Panics
/// This function panics if the underlying lock has been poisoned and might panic if the global config is already held by the current thread.
pub fn global_config_mut() -> RwLockWriteGuard<'static, Config> {
CONFIG
.get_or_init(|| RwLock::new(Config::default()))
.write()
.unwrap()
CONFIG.write().unwrap()
}

/// The metadata version to retrieve.
Expand Down

0 comments on commit 7c38c8d

Please sign in to comment.