summaryrefslogtreecommitdiffstats
path: root/src/lib.rs
diff options
context:
space:
mode:
authorRyan Leckey <leckey.ryan@gmail.com>2017-01-28 22:40:03 -0800
committerRyan Leckey <leckey.ryan@gmail.com>2017-01-28 22:40:03 -0800
commit56aed8323655769ebd0e6e1bd3c40775308e62a7 (patch)
tree9ff60d4263eeb61aef3223e3b6dac8746a7ff952 /src/lib.rs
parentc1bcf6ec5537bfd0fb3f1ee8d93a32aa15031cff (diff)
Add back RwLock
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs52
1 files changed, 41 insertions, 11 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 25e2255..9cb8f82 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -15,7 +15,7 @@ mod env;
mod config;
use std::error::Error;
-use std::sync::{Once, ONCE_INIT};
+use std::sync::{Once, ONCE_INIT, RwLock};
use std::borrow::Cow;
pub use source::{Source, SourceBuilder};
@@ -27,11 +27,11 @@ pub use value::Value;
pub use config::Config;
// Global configuration
-static mut CONFIG: Option<Config<'static>> = None;
+static mut CONFIG: Option<RwLock<Config<'static>>> = None;
static CONFIG_INIT: Once = ONCE_INIT;
// Get the global configuration instance
-pub fn global() -> &'static mut Config<'static> {
+pub fn global() -> &'static mut RwLock<Config<'static>> {
unsafe {
CONFIG_INIT.call_once(|| {
CONFIG = Some(Default::default());
@@ -44,37 +44,67 @@ pub fn global() -> &'static mut Config<'static> {
pub fn merge<T>(source: T) -> Result<(), Box<Error>>
where T: SourceBuilder
{
- global().merge(source)
+ global().write().unwrap().merge(source)
}
pub fn set_default<T>(key: &str, value: T) -> Result<(), Box<Error>>
where T: Into<Value<'static>>
{
- global().set_default(key, value)
+ global().write().unwrap().set_default(key, value)
}
pub fn set<T>(key: &str, value: T) -> Result<(), Box<Error>>
where T: Into<Value<'static>>
{
- global().set(key, value)
+ global().write().unwrap().set(key, value)
}
pub fn get<'a>(key: &str) -> Option<Cow<'a, Value>> {
- global().get(key)
+ // TODO(~): Should this panic! or return None with an error message?
+ // Make an issue if you think it should be an error message.
+ let r = global().read().unwrap();
+
+ let c = &*r;
+
+ // TODO(@rust): Figure out how to not to use unsafe here
+ unsafe {
+ let c: &'static Config = std::mem::transmute(c);
+ c.get(key)
+ }
}
pub fn get_str<'a>(key: &str) -> Option<Cow<'a, str>> {
- global().get_str(key)
+ let r = global().read().unwrap();
+
+ unsafe {
+ let c: &'static Config = std::mem::transmute(&*r);
+ c.get_str(key)
+ }
}
pub fn get_int(key: &str) -> Option<i64> {
- global().get_int(key)
+ let r = global().read().unwrap();
+
+ unsafe {
+ let c: &'static Config = std::mem::transmute(&*r);
+ c.get_int(key)
+ }
}
pub fn get_float(key: &str) -> Option<f64> {
- global().get_float(key)
+ let r = global().read().unwrap();
+
+ unsafe {
+ let c: &'static Config = std::mem::transmute(&*r);
+ c.get_float(key)
+ }
}
pub fn get_bool(key: &str) -> Option<bool> {
- global().get_bool(key)
+ let r = global().read().unwrap();
+
+ unsafe {
+ let c: &'static Config = std::mem::transmute(&*r);
+ c.get_bool(key)
+ }
}