diff options
author | Ryan Leckey <leckey.ryan@gmail.com> | 2017-01-28 20:46:29 -0800 |
---|---|---|
committer | Ryan Leckey <leckey.ryan@gmail.com> | 2017-01-28 20:47:26 -0800 |
commit | 7d870758cbd9ad4181471ad40184d1bac1204e1e (patch) | |
tree | 5f84b003c3dfe4c65b5ed97cbce8de6b8f984c49 /src/lib.rs | |
parent | 0b3087a8cbe6c0b9428d33c3a2c92155a3bbc558 (diff) |
Use 'Cow' to remove unnecessary allocations
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 42 |
1 files changed, 20 insertions, 22 deletions
@@ -1,4 +1,5 @@ #![feature(drop_types_in_const)] + #![allow(unknown_lints)] #[cfg(feature = "toml")] @@ -14,7 +15,8 @@ mod env; mod config; use std::error::Error; -use std::sync::{Once, ONCE_INIT, RwLock}; +use std::sync::{Once, ONCE_INIT}; +use std::borrow::Cow; pub use source::{Source, SourceBuilder}; pub use file::{File, FileFormat}; @@ -25,13 +27,15 @@ pub use value::Value; pub use config::Config; // Global configuration -static mut CONFIG: Option<RwLock<Config>> = None; +static mut CONFIG: Option<Config<'static>> = None; static CONFIG_INIT: Once = ONCE_INIT; // Get the global configuration instance -fn global() -> &'static RwLock<Config> { +pub fn global() -> &'static mut Config<'static> { unsafe { - CONFIG_INIT.call_once(|| { CONFIG = Some(Default::default()); }); + CONFIG_INIT.call_once(|| { + CONFIG = Some(Default::default()); + }); CONFIG.as_mut().unwrap() } @@ -40,43 +44,37 @@ fn global() -> &'static RwLock<Config> { pub fn merge<T>(source: T) -> Result<(), Box<Error>> where T: SourceBuilder { - global().write()?.merge(source) + global().merge(source) } pub fn set_default<T>(key: &str, value: T) -> Result<(), Box<Error>> - where T: Into<Value> + where T: Into<Value<'static>> { - global().write()?.set_default(key, value); - - // TODO: `set_default` will be able to fail soon so this will not be needed - Ok(()) + global().set_default(key, value) } pub fn set<T>(key: &str, value: T) -> Result<(), Box<Error>> - where T: Into<Value> + where T: Into<Value<'static>> { - global().write()?.set(key, value); - - // TODO: `set_default` will be able to fail soon so this will not be needed - Ok(()) + global().set(key, value) } -pub fn get(key: &str) -> Option<Value> { - global().read().unwrap().get(key) +pub fn get<'a>(key: &str) -> Option<Cow<'a, Value>> { + global().get(key) } -pub fn get_str(key: &str) -> Option<String> { - global().read().unwrap().get_str(key) +pub fn get_str<'a>(key: &str) -> Option<Cow<'a, str>> { + global().get_str(key) } pub fn get_int(key: &str) -> Option<i64> { - global().read().unwrap().get_int(key) + global().get_int(key) } pub fn get_float(key: &str) -> Option<f64> { - global().read().unwrap().get_float(key) + global().get_float(key) } pub fn get_bool(key: &str) -> Option<bool> { - global().read().unwrap().get_bool(key) + global().get_bool(key) } |