#[repr(transparent)]pub struct Variant { /* private fields */ }
Expand description
A generic immutable value capable of carrying various types.
See the module documentation for more details.
Implementations§
source§impl Variant
impl Variant
sourcepub fn is<T: StaticVariantType>(&self) -> bool
pub fn is<T: StaticVariantType>(&self) -> bool
Returns true
if the type of the value corresponds to T
.
sourcepub fn is_type(&self, type_: &VariantTy) -> bool
pub fn is_type(&self, type_: &VariantTy) -> bool
Returns true
if the type of the value corresponds to type_
.
This is equivalent to self.type_().is_subtype_of(type_)
.
sourcepub fn classify(&self) -> VariantClass
pub fn classify(&self) -> VariantClass
Returns the classification of the variant.
sourcepub fn get<T: FromVariant>(&self) -> Option<T>
pub fn get<T: FromVariant>(&self) -> Option<T>
Tries to extract a value of type T
.
Returns Some
if T
matches the variant’s type.
sourcepub fn try_get<T: FromVariant>(&self) -> Result<T, VariantTypeMismatchError>
pub fn try_get<T: FromVariant>(&self) -> Result<T, VariantTypeMismatchError>
Tries to extract a value of type T
.
sourcepub fn from_variant(value: &Variant) -> Self
pub fn from_variant(value: &Variant) -> Self
Boxes value.
sourcepub fn as_variant(&self) -> Option<Variant>
pub fn as_variant(&self) -> Option<Variant>
Unboxes self.
Returns Some
if self contains a Variant
.
sourcepub fn child_value(&self, index: usize) -> Variant
pub fn child_value(&self, index: usize) -> Variant
Reads a child item out of a container Variant
instance.
Panics
- if
self
is not a container type. - if given
index
is larger than number of children.
sourcepub fn try_child_value(&self, index: usize) -> Option<Variant>
pub fn try_child_value(&self, index: usize) -> Option<Variant>
Try to read a child item out of a container Variant
instance.
It returns None
if self
is not a container type or if the given
index
is larger than number of children.
sourcepub fn try_child_get<T: StaticVariantType + FromVariant>(
&self,
index: usize
) -> Result<Option<T>, VariantTypeMismatchError>
pub fn try_child_get<T: StaticVariantType + FromVariant>( &self, index: usize ) -> Result<Option<T>, VariantTypeMismatchError>
Try to read a child item out of a container Variant
instance.
It returns Ok(None)
if self
is not a container type or if the given
index
is larger than number of children. An error is thrown if the
type does not match.
sourcepub fn child_get<T: StaticVariantType + FromVariant>(&self, index: usize) -> T
pub fn child_get<T: StaticVariantType + FromVariant>(&self, index: usize) -> T
Read a child item out of a container Variant
instance.
Panics
- if
self
is not a container type. - if given
index
is larger than number of children. - if the expected variant type does not match
sourcepub fn str(&self) -> Option<&str>
pub fn str(&self) -> Option<&str>
Tries to extract a &str
.
Returns Some
if the variant has a string type (s
, o
or g
type
strings).
sourcepub fn fixed_array<T: FixedSizeVariantType>(
&self
) -> Result<&[T], VariantTypeMismatchError>
pub fn fixed_array<T: FixedSizeVariantType>( &self ) -> Result<&[T], VariantTypeMismatchError>
Tries to extract a &[T]
from a variant of array type with a suitable element type.
Returns an error if the type is wrong.
sourcepub fn array_from_iter<T: StaticVariantType>(
children: impl IntoIterator<Item = Variant>
) -> Self
pub fn array_from_iter<T: StaticVariantType>( children: impl IntoIterator<Item = Variant> ) -> Self
Creates a new Variant array from children.
Panics
This function panics if not all variants are of type T
.
sourcepub fn array_from_iter_with_type(
type_: &VariantTy,
children: impl IntoIterator<Item = impl AsRef<Variant>>
) -> Self
pub fn array_from_iter_with_type( type_: &VariantTy, children: impl IntoIterator<Item = impl AsRef<Variant>> ) -> Self
Creates a new Variant array from children with the specified type.
Panics
This function panics if not all variants are of type type_
.
sourcepub fn array_from_fixed_array<T: FixedSizeVariantType>(array: &[T]) -> Self
pub fn array_from_fixed_array<T: FixedSizeVariantType>(array: &[T]) -> Self
Creates a new Variant array from a fixed array.
sourcepub fn tuple_from_iter(
children: impl IntoIterator<Item = impl AsRef<Variant>>
) -> Self
pub fn tuple_from_iter( children: impl IntoIterator<Item = impl AsRef<Variant>> ) -> Self
Creates a new Variant tuple from children.
sourcepub fn from_dict_entry(key: &Variant, value: &Variant) -> Self
pub fn from_dict_entry(key: &Variant, value: &Variant) -> Self
Creates a new dictionary entry Variant.
DictEntry should be preferred over this when the types are known statically.
sourcepub fn from_maybe<T: StaticVariantType>(child: Option<&Variant>) -> Self
pub fn from_maybe<T: StaticVariantType>(child: Option<&Variant>) -> Self
Creates a new maybe Variant.
sourcepub fn as_maybe(&self) -> Option<Variant>
pub fn as_maybe(&self) -> Option<Variant>
Extract the value of a maybe Variant.
Returns the child value, or None
if the value is Nothing.
Panics
Panics if compiled with debug_assertions
and the variant is not maybe-typed.
sourcepub fn print(&self, type_annotate: bool) -> GString
pub fn print(&self, type_annotate: bool) -> GString
Pretty-print the contents of this variant in a human-readable form.
A variant can be recreated from this output via Variant::parse
.
sourcepub fn parse(type_: Option<&VariantTy>, text: &str) -> Result<Self, Error>
pub fn parse(type_: Option<&VariantTy>, text: &str) -> Result<Self, Error>
Parses a GVariant from the text representation produced by print()
.
sourcepub fn from_bytes<T: StaticVariantType>(bytes: &Bytes) -> Self
pub fn from_bytes<T: StaticVariantType>(bytes: &Bytes) -> Self
Constructs a new serialized-mode GVariant instance.
sourcepub unsafe fn from_bytes_trusted<T: StaticVariantType>(bytes: &Bytes) -> Self
pub unsafe fn from_bytes_trusted<T: StaticVariantType>(bytes: &Bytes) -> Self
Constructs a new serialized-mode GVariant instance.
This is the same as from_bytes
, except that checks on the passed
data are skipped.
You should not use this function on data from external sources.
Safety
Since the data is not validated, this is potentially dangerous if called on bytes which are not guaranteed to have come from serialising another Variant. The caller is responsible for ensuring bad data is not passed in.
sourcepub fn from_data<T: StaticVariantType, A: AsRef<[u8]>>(data: A) -> Self
pub fn from_data<T: StaticVariantType, A: AsRef<[u8]>>(data: A) -> Self
Constructs a new serialized-mode GVariant instance.
sourcepub unsafe fn from_data_trusted<T: StaticVariantType, A: AsRef<[u8]>>(
data: A
) -> Self
pub unsafe fn from_data_trusted<T: StaticVariantType, A: AsRef<[u8]>>( data: A ) -> Self
Constructs a new serialized-mode GVariant instance.
This is the same as from_data
, except that checks on the passed
data are skipped.
You should not use this function on data from external sources.
Safety
Since the data is not validated, this is potentially dangerous if called on bytes which are not guaranteed to have come from serialising another Variant. The caller is responsible for ensuring bad data is not passed in.
sourcepub fn from_bytes_with_type(bytes: &Bytes, type_: &VariantTy) -> Self
pub fn from_bytes_with_type(bytes: &Bytes, type_: &VariantTy) -> Self
Constructs a new serialized-mode GVariant instance with a given type.
sourcepub unsafe fn from_bytes_with_type_trusted(
bytes: &Bytes,
type_: &VariantTy
) -> Self
pub unsafe fn from_bytes_with_type_trusted( bytes: &Bytes, type_: &VariantTy ) -> Self
Constructs a new serialized-mode GVariant instance with a given type.
This is the same as from_bytes
, except that checks on the passed
data are skipped.
You should not use this function on data from external sources.
Safety
Since the data is not validated, this is potentially dangerous if called on bytes which are not guaranteed to have come from serialising another Variant. The caller is responsible for ensuring bad data is not passed in.
sourcepub fn from_data_with_type<A: AsRef<[u8]>>(data: A, type_: &VariantTy) -> Self
pub fn from_data_with_type<A: AsRef<[u8]>>(data: A, type_: &VariantTy) -> Self
Constructs a new serialized-mode GVariant instance with a given type.
sourcepub unsafe fn from_data_with_type_trusted<A: AsRef<[u8]>>(
data: A,
type_: &VariantTy
) -> Self
pub unsafe fn from_data_with_type_trusted<A: AsRef<[u8]>>( data: A, type_: &VariantTy ) -> Self
Constructs a new serialized-mode GVariant instance with a given type.
This is the same as from_data
, except that checks on the passed
data are skipped.
You should not use this function on data from external sources.
Safety
Since the data is not validated, this is potentially dangerous if called on bytes which are not guaranteed to have come from serialising another Variant. The caller is responsible for ensuring bad data is not passed in.
sourcepub fn data_as_bytes(&self) -> Bytes
pub fn data_as_bytes(&self) -> Bytes
Returns the serialized form of a GVariant instance.
sourcepub fn store(&self, data: &mut [u8]) -> Result<usize, BoolError>
pub fn store(&self, data: &mut [u8]) -> Result<usize, BoolError>
Stores the serialized form of a GVariant instance into the given slice.
The slice needs to be big enough.
sourcepub fn normal_form(&self) -> Self
pub fn normal_form(&self) -> Self
Returns a copy of the variant in normal form.
sourcepub fn n_children(&self) -> usize
pub fn n_children(&self) -> usize
Determines the number of children in a container GVariant instance.
sourcepub fn iter(&self) -> VariantIter ⓘ
pub fn iter(&self) -> VariantIter ⓘ
Create an iterator over items in the variant.
Note that this heap allocates a variant for each element, which can be particularly expensive for large arrays.
sourcepub fn array_iter_str(
&self
) -> Result<VariantStrIter<'_>, VariantTypeMismatchError>
pub fn array_iter_str( &self ) -> Result<VariantStrIter<'_>, VariantTypeMismatchError>
Create an iterator over borrowed strings from a GVariant of type as
(array of string).
This will fail if the variant is not an array of with the expected child type.
A benefit of this API over Self::iter()
is that it
minimizes allocation, and provides strongly typed access.
let strs = &["foo", "bar"];
let strs_variant: glib::Variant = strs.to_variant();
for s in strs_variant.array_iter_str()? {
println!("{}", s);
}
sourcepub fn is_container(&self) -> bool
pub fn is_container(&self) -> bool
Return whether this Variant is a container type.
sourcepub fn is_normal_form(&self) -> bool
pub fn is_normal_form(&self) -> bool
Return whether this Variant is in normal form.
sourcepub fn is_object_path(string: &str) -> bool
pub fn is_object_path(string: &str) -> bool
Return whether input string is a valid VariantClass::ObjectPath
.
sourcepub fn is_signature(string: &str) -> bool
pub fn is_signature(string: &str) -> bool
Return whether input string is a valid VariantClass::Signature
.
Trait Implementations§
source§impl From<Variant> for VariantDict
impl From<Variant> for VariantDict
source§impl<T: ToVariant + StaticVariantType> FromIterator<T> for Variant
impl<T: ToVariant + StaticVariantType> FromIterator<T> for Variant
source§fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
source§impl FromVariant for Variant
impl FromVariant for Variant
source§impl PartialEq<Variant> for Variant
impl PartialEq<Variant> for Variant
source§impl PartialOrd<Variant> for Variant
impl PartialOrd<Variant> for Variant
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl StaticType for Variant
impl StaticType for Variant
source§fn static_type() -> Type
fn static_type() -> Type
Self
.source§impl StaticVariantType for Variant
impl StaticVariantType for Variant
source§fn static_variant_type() -> Cow<'static, VariantTy>
fn static_variant_type() -> Cow<'static, VariantTy>
VariantType
corresponding to Self
.source§impl ToVariant for Variant
impl ToVariant for Variant
source§fn to_variant(&self) -> Variant
fn to_variant(&self) -> Variant
Variant
clone of self
.impl Eq for Variant
impl Send for Variant
impl Sync for Variant
Auto Trait Implementations§
Blanket Implementations§
source§impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GList> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GList> for Twhere T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
source§impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GPtrArray> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GPtrArray> for Twhere T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
source§impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GSList> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GSList> for Twhere T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
source§impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GList> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GList> for Twhere T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
source§impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GPtrArray> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GPtrArray> for Twhere T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
source§impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GSList> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GSList> for Twhere T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
source§impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GList> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GList> for Twhere T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
source§impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GPtrArray> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GPtrArray> for Twhere T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
source§impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GSList> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GSList> for Twhere T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
source§impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GList> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GList> for Twhere T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
source§impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GPtrArray> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GPtrArray> for Twhere T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
source§impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GSList> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GSList> for Twhere T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
source§impl<T> StaticTypeExt for Twhere
T: StaticType,
impl<T> StaticTypeExt for Twhere T: StaticType,
source§fn ensure_type()
fn ensure_type()
source§impl<T> ToClosureReturnValue for Twhere
T: ToValue,
impl<T> ToClosureReturnValue for Twhere T: ToValue,
fn to_closure_return_value(&self) -> Option<Value>
source§impl<T> ToSendValue for Twhere
T: Send + ToValue + ?Sized,
impl<T> ToSendValue for Twhere T: Send + ToValue + ?Sized,
source§fn to_send_value(&self) -> SendValue
fn to_send_value(&self) -> SendValue
SendValue
clone of self
.