Struct libspa::utils::dict::StaticDict
source · pub struct StaticDict { /* private fields */ }
Expand description
A collection of static key/value pairs.
§Examples
Create a StaticDict
and access the stored values by key:
use libspa::{utils::dict::StaticDict, static_dict};
static DICT: StaticDict = static_dict!{
"Key" => "Value",
"OtherKey" => "OtherValue"
};
assert_eq!(Some("Value"), DICT.get("Key"));
assert_eq!(Some("OtherValue"), DICT.get("OtherKey"));
Implementations§
source§impl StaticDict
impl StaticDict
sourcepub const unsafe fn from_ptr(ptr: NonNull<spa_dict>) -> Self
pub const unsafe fn from_ptr(ptr: NonNull<spa_dict>) -> Self
Create a StaticDict
from an existing raw spa_dict
pointer.
§Safety
- The provided pointer must point to a valid, well-aligned
spa_dict
struct. - The struct and its content need to stay alive during the whole lifetime of the
StaticDict
. - The keys and values stored in this dict have to be static strings.
Methods from Deref<Target = DictRef>§
sourcepub fn as_raw(&self) -> &spa_dict
pub fn as_raw(&self) -> &spa_dict
Returns a reference to the raw spa_sys::spa_dict
this struct represents.
sourcepub fn as_raw_ptr(&self) -> *mut spa_dict
pub fn as_raw_ptr(&self) -> *mut spa_dict
Returns the pointer to the raw spa_sys::spa_dict
this struct represents.
§Safety
The returned pointer must not be used after the DictRef
reference this method is called on becomes invalid.
sourcepub fn iter_cstr(&self) -> CIter<'_> ⓘ
pub fn iter_cstr(&self) -> CIter<'_> ⓘ
An iterator over all key-value pairs as (&CStr, &CStr)
tuples.
Use iter
to iterate over all valid utf-8 pairs as (&str, &str) tuples instead.
sourcepub fn iter(&self) -> Iter<'_> ⓘ
pub fn iter(&self) -> Iter<'_> ⓘ
An iterator over all key-value pairs that are valid utf-8.
The iterator element type is (&str, &str)
.
sourcepub fn keys(&self) -> Keys<'_> ⓘ
pub fn keys(&self) -> Keys<'_> ⓘ
An iterator over all keys that are valid utf-8. The iterator element type is &str.
sourcepub fn values(&self) -> Values<'_> ⓘ
pub fn values(&self) -> Values<'_> ⓘ
An iterator over all values that are valid utf-8. The iterator element type is &str.
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of key-value-pairs in the dict. This is the number of all pairs, not only pairs that are valid-utf8.
sourcepub fn get(&self, key: &str) -> Option<&str>
pub fn get(&self, key: &str) -> Option<&str>
Get the value associated with the provided key.
If the dict does not contain the key or the value is non-utf8, None
is returned.
Use iter_cstr
if you need a non-utf8 key or value.
sourcepub fn parse<T: ParsableValue>(
&self,
key: &str
) -> Option<Result<T, ParseValueError>>
pub fn parse<T: ParsableValue>( &self, key: &str ) -> Option<Result<T, ParseValueError>>
Get the value associated with the provided key and convert it to a given type.
If the dict does not contain the key or the value is non-utf8, None
is returned.
If the value associated with the key cannot be parsed to the requested type,
Some(Err(ParseValueError))
is returned.
See ParsableValue
for all the types which can be produced by this method.
§Examples
use libspa::{utils::dict::StaticDict, static_dict};
static DICT: StaticDict = static_dict! {
"true" => "true",
"ten" => "10",
"pi" => "3.14159265359",
"pointer" => "pointer:0xdeadbeef"
};
assert_eq!(DICT.parse("true"), Some(Ok(true)));
assert_eq!(DICT.parse("ten"), Some(Ok(10)));
assert_eq!(DICT.parse("ten"), Some(Ok(10.0)));
assert_eq!(DICT.parse("pi"), Some(Ok(3.14159265359)));
let ptr = DICT.parse::<*const i32>("pointer").unwrap().unwrap();
assert!(!ptr.is_null());