pub enum KvValue {
Float(f64),
Int(i64),
Uint(u64),
Bool(bool),
Str(String),
}Expand description
Types available for use in KV.
Float, Int and Uint are all 64bit. The corresponding 32bit variants must be
be converted to 64 bit. Preferably the From impls are used to create a KvValue:
let x: KvValue = 1u64.into();
assert_eq!(x, KvValue::Uint(1u64));
let x: KvValue = 2u32.into();
assert_eq!(x, KvValue::Uint(2u64));
let x: KvValue = 2i64.into();
assert_eq!(x, KvValue::Int(2i64));
let x: KvValue = 2i32.into();
assert_eq!(x, KvValue::Int(2i64));
let x: KvValue = 1.0f64.into();
assert_eq!(x, KvValue::Float(1f64));
let x: KvValue = 1.0f32.into();
assert_eq!(x, KvValue::Float(1f64));
let x: KvValue = true.into();
assert_eq!(x, KvValue::Bool(true));
let x: KvValue = "a str".into();
assert_eq!(x, KvValue::Str("a str".to_string()));
let x: KvValue = "a String".to_string().into();
assert_eq!(x, KvValue::Str("a String".to_string()));Variants§
Float(f64)
Floating point values
Int(i64)
Signed integers
Uint(u64)
Unsigned integers
Bool(bool)
Boolean values
Str(String)
Strings
Implementations§
Source§impl KvValue
impl KvValue
Sourcepub fn kind(&self) -> &'static str
pub fn kind(&self) -> &'static str
Returns the kind of the KvValue
§Examples
assert_eq!(KvValue::Float(1.0).kind(), "Float");
assert_eq!(KvValue::Int(1).kind(), "Int");
assert_eq!(KvValue::Uint(1).kind(), "Uint");
assert_eq!(KvValue::Bool(true).kind(), "Bool");
assert_eq!(KvValue::Str("string".to_string()).kind(), "Str");Sourcepub fn get_float(&self) -> Option<f64>
pub fn get_float(&self) -> Option<f64>
Extract float from KvValue
Returns Some(<float>) if KvValue is of kind Float.
Note: For KvValue::Int and KvValue::Uintinteger values are cast to f64, therefore
this operation may be lossy!
KvValue::Bool is turned into 1.0f64 if true and 0.0f64 if false.
§Example
assert_eq!(KvValue::Float(1.0).get_float(), Some(1.0));
assert_eq!(KvValue::Int(1).get_float(), Some(1.0));
assert_eq!(KvValue::Uint(1).get_float(), Some(1.0));
assert_eq!(KvValue::Bool(true).get_float(), Some(1.0));
assert_eq!(KvValue::Str("not a number".to_string()).get_float(), None);Sourcepub fn get_int(&self) -> Option<i64>
pub fn get_int(&self) -> Option<i64>
Extract int from KvValue
Returns Some(<int>) if KvValue is of kind Int and None otherwise.
§Example
assert_eq!(KvValue::Int(1).get_int(), Some(1i64));
assert_eq!(KvValue::Float(1.0).get_int(), None);
assert_eq!(KvValue::Uint(1).get_int(), None);
assert_eq!(KvValue::Bool(true).get_int(), None);
assert_eq!(KvValue::Str("not an int".to_string()).get_int(), None);Sourcepub fn get_uint(&self) -> Option<u64>
pub fn get_uint(&self) -> Option<u64>
Extract unsigned int from KvValue
Returns Some(<unsigned int>) if KvValue is of kind Uint and None otherwise.
§Example
assert_eq!(KvValue::Uint(1).get_uint(), Some(1u64));
assert_eq!(KvValue::Int(1).get_uint(), None);
assert_eq!(KvValue::Float(1.0).get_uint(), None);
assert_eq!(KvValue::Bool(true).get_uint(), None);
assert_eq!(KvValue::Str("not an uint".to_string()).get_uint(), None);Sourcepub fn get_bool(&self) -> Option<bool>
pub fn get_bool(&self) -> Option<bool>
Extract bool from KvValue
Returns Some(<bool>) if KvValue is of kind Bool and None otherwise.
§Example
assert_eq!(KvValue::Bool(true).get_bool(), Some(true));
assert_eq!(KvValue::Float(1.0).get_bool(), None);
assert_eq!(KvValue::Int(1).get_bool(), None);
assert_eq!(KvValue::Uint(1).get_bool(), None);
assert_eq!(KvValue::Str("not a bool".to_string()).get_bool(), None);Sourcepub fn get_string(&self) -> Option<String>
pub fn get_string(&self) -> Option<String>
Extract String from KvValue
Returns Some(<string>) if KvValue is of kind Str and None otherwise.
§Example
assert_eq!(KvValue::Str("a string".to_string()).get_string(), Some("a string".to_string()));
assert_eq!(KvValue::Float(1.0).get_string(), None);
assert_eq!(KvValue::Int(1).get_string(), None);
assert_eq!(KvValue::Uint(1).get_string(), None);
assert_eq!(KvValue::Bool(true).get_string(), None);Sourcepub fn as_string(&self) -> String
pub fn as_string(&self) -> String
Get String representation of KvValue
§Example
assert_eq!(KvValue::Str("a string".to_string()).as_string(), "a string".to_string());
assert_eq!(KvValue::Float(1.0).as_string(), "1".to_string());
assert_eq!(KvValue::Int(1).as_string(), "1".to_string());
assert_eq!(KvValue::Uint(1).as_string(), "1".to_string());
assert_eq!(KvValue::Bool(true).as_string(), "true".to_string());