Struct glib::VariantDict
source · #[repr(transparent)]pub struct VariantDict { /* private fields */ }
Expand description
VariantDict
is a mutable key/value store where the keys are always
strings and the values are Variant
s.
Variant dictionaries can easily be converted to/from Variant
s of the
appropriate type. In glib
terms, this is a variant of the form "a{sv}"
.
Panics
Note, pretty much all methods on this struct will panic if the
end_unsafe()
method was called on the instance.
Implementations§
source§impl VariantDict
impl VariantDict
sourcepub fn new(from_asv: Option<&Variant>) -> Self
pub fn new(from_asv: Option<&Variant>) -> Self
Create a new VariantDict
optionally populating it with the given Variant
Since Variant
s are immutable, this does not couple the VariantDict
with
the input Variant
, instead the contents are copied into the VariantDict
.
Panics
This function will panic if the given Variant
is not of the correct type.
sourcepub fn contains(&self, key: &str) -> bool
pub fn contains(&self, key: &str) -> bool
Check if this VariantDict
contains the given key.
Look up whether or not the given key is present, returning true
if it
is present in self
.
sourcepub fn lookup<T: FromVariant>(
&self,
key: &str
) -> Result<Option<T>, VariantTypeMismatchError>
pub fn lookup<T: FromVariant>( &self, key: &str ) -> Result<Option<T>, VariantTypeMismatchError>
Look up a typed value from this VariantDict
.
The given key
is looked up in self
.
This will return None
if the key
is not present in the dictionary,
and an error if the key is present but with the wrong type.
sourcepub fn lookup_value(
&self,
key: &str,
expected_type: Option<&VariantTy>
) -> Option<Variant>
pub fn lookup_value( &self, key: &str, expected_type: Option<&VariantTy> ) -> Option<Variant>
Look up and return a value from this VariantDict
.
The given key
is looked up in self
. If expected_type
is not
None
then it will be matched against the type of any found value.
This will return None
if the key
is not present in the dictionary
or if it is present but the type of the value does not match a given
expected_type
. Otherwise, Some(value)
will be returned where
the value
is an instance of Variant
.
sourcepub fn insert_value(&self, key: &str, value: &Variant)
pub fn insert_value(&self, key: &str, value: &Variant)
sourcepub fn insert<T: ToVariant>(&self, key: &str, value: &T)
pub fn insert<T: ToVariant>(&self, key: &str, value: &T)
Insert a value into the dictionary
The given key
/value
pair is inserted into self
. If a value
was previously associated with key
then it is overwritten.
This is a convenience method which automatically calls
to_variant()
for you
on the given value.
If, on the other hand, you have a Variant
instance already, you should use the insert_value()
method instead.
sourcepub fn remove(&self, key: &str) -> bool
pub fn remove(&self, key: &str) -> bool
Remove the given key
from the dictionary.
This removes the given key
from the dictionary, releasing the reference
on the associated value if one is present.
If a key
/value
pair was removed from the dictionary, true
is
returned. If key
was not present then false
is returned instead.
sourcepub unsafe fn end_unsafe(&self) -> Variant
pub unsafe fn end_unsafe(&self) -> Variant
Convert this dictionary to a Variant
This method converts self
into an instance of Variant
but in doing so renders it very unsafe to use.
Safety
After calling this, the underlying GVariantDict
is in a state where
the only valid operations to perform as reference ones. As such
any attempt to read/update the dictionary will fail and emit warnings
of such.
You should only use this function if the extra cost of the safe function is too much for your performance critical codepaths
sourcepub fn end(&self) -> Variant
pub fn end(&self) -> Variant
Convert this dictionary to a Variant
This method converts self
into an instance of Variant
and then reinitialises itself in order to be safe for further use.
If you are certain that nothing other than disposing of references will
be done after ending the instance, you can call the
end_unsafe()
method instead to avoid the unnecessary
reinitialisation of the dictionary.
Trait Implementations§
source§impl Clone for VariantDict
impl Clone for VariantDict
source§impl Default for VariantDict
impl Default for VariantDict
source§impl From<Variant> for VariantDict
impl From<Variant> for VariantDict
source§impl FromVariant for VariantDict
impl FromVariant for VariantDict
source§impl StaticType for VariantDict
impl StaticType for VariantDict
source§fn static_type() -> Type
fn static_type() -> Type
Self
.source§impl StaticVariantType for VariantDict
impl StaticVariantType for VariantDict
source§fn static_variant_type() -> Cow<'static, VariantTy>
fn static_variant_type() -> Cow<'static, VariantTy>
VariantType
corresponding to Self
.source§impl ToVariant for VariantDict
impl ToVariant for VariantDict
source§fn to_variant(&self) -> Variant
fn to_variant(&self) -> Variant
Variant
clone of self
.