From bcd0737e15726f3ad324c3cf26c5d04fe5f75766 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Sat, 11 Feb 2017 12:00:23 -0800 Subject: Move to copy API instead of reference; fixes #9 --- src/lib.rs | 52 ++++++++++++++++------------------------------------ 1 file changed, 16 insertions(+), 36 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 073815f..dae615c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -30,7 +30,7 @@ //! ```rust //! // Get 'debug' and coerce to a boolean //! if let Some(value) = config::get("debug") { -//! println!("{:?}", value.as_bool()); +//! println!("{:?}", value.into_bool()); //! } //! //! // You can use a type suffix @@ -62,7 +62,7 @@ mod config; use std::error::Error; use std::sync::{Once, ONCE_INIT, RwLock}; -use std::borrow::Cow; +use std::collections::HashMap; pub use source::{Source, SourceBuilder}; pub use file::{File, FileFormat}; @@ -105,52 +105,32 @@ pub fn set(key: &str, value: T) -> Result<(), Box> global().write().unwrap().set(key, value) } -pub fn get<'a>(key: &str) -> Option<&'a Value> { +pub fn get(key: &str) -> Option { // 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) - } + global().read().unwrap().get(key) } -pub fn get_str<'a>(key: &str) -> Option> { - let r = global().read().unwrap(); - - unsafe { - let c: &'static Config = std::mem::transmute(&*r); - c.get_str(key) - } +pub fn get_str(key: &str) -> Option { + get(key).and_then(Value::into_str) } pub fn get_int(key: &str) -> Option { - let r = global().read().unwrap(); - - unsafe { - let c: &'static Config = std::mem::transmute(&*r); - c.get_int(key) - } + get(key).and_then(Value::into_int) } pub fn get_float(key: &str) -> Option { - let r = global().read().unwrap(); - - unsafe { - let c: &'static Config = std::mem::transmute(&*r); - c.get_float(key) - } + get(key).and_then(Value::into_float) } pub fn get_bool(key: &str) -> Option { - let r = global().read().unwrap(); + get(key).and_then(Value::into_bool) +} - unsafe { - let c: &'static Config = std::mem::transmute(&*r); - c.get_bool(key) - } +pub fn get_table(key: &str) -> Option> { + get(key).and_then(Value::into_table) +} + +pub fn get_array(key: &str) -> Option> { + get(key).and_then(Value::into_array) } -- cgit v1.2.3