From 7d870758cbd9ad4181471ad40184d1bac1204e1e Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Sat, 28 Jan 2017 20:46:29 -0800 Subject: Use 'Cow' to remove unnecessary allocations --- src/lib.rs | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index ad527e2..25e2255 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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> = None; +static mut CONFIG: Option> = None; static CONFIG_INIT: Once = ONCE_INIT; // Get the global configuration instance -fn global() -> &'static RwLock { +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 { pub fn merge(source: T) -> Result<(), Box> where T: SourceBuilder { - global().write()?.merge(source) + global().merge(source) } pub fn set_default(key: &str, value: T) -> Result<(), Box> - where T: Into + where T: Into> { - 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(key: &str, value: T) -> Result<(), Box> - where T: Into + where T: Into> { - 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 { - global().read().unwrap().get(key) +pub fn get<'a>(key: &str) -> Option> { + global().get(key) } -pub fn get_str(key: &str) -> Option { - global().read().unwrap().get_str(key) +pub fn get_str<'a>(key: &str) -> Option> { + global().get_str(key) } pub fn get_int(key: &str) -> Option { - global().read().unwrap().get_int(key) + global().get_int(key) } pub fn get_float(key: &str) -> Option { - global().read().unwrap().get_float(key) + global().get_float(key) } pub fn get_bool(key: &str) -> Option { - global().read().unwrap().get_bool(key) + global().get_bool(key) } -- cgit v1.2.3