#[repr(transparent)]pub struct Variant(_);
Expand description
The GVariant Variant v type
The Variant type can contain any GVariant value.
Non-spec conformant implementation of Equality with non-normal form data
While every value has a single canoncial byte representation (“normal form”) there other representations that have the same value. For example: values of type (yi) have 3B of padding between the y and the i. In normal form these bytes are 0, but they are irrelevant for the actual value. Ignoring the value of the padding bytes is correct according to the spec.
This is handled correctly when comparing the values of two (yi)
instances in rust code. What isn’t correct is the handling of Variant
v types that contain non-normal data with respect to checking for
equality. Correct checking of equality would require deserialising the data
according to the typestr contained within the variant, and then doing the
comparison. We don’t do run-time interpretation of typestrs in this crate,
prefering to do it at compile time. Instead we just compare the underlying
data. This gives correct results for data in normal form, but there will be
some false-negatives for non-normal form data.
Therefore Variant
implements PartialEq
, but not Eq
because the
comparison is not “reflexive”.
Implementations
sourceimpl Variant
impl Variant
sourcepub fn get<M: Marker>(&self, m: M) -> Option<&M::Type> where
AlignedSlice<A8>: AsAligned<<M::Type as AlignOf>::AlignOf>,
pub fn get<M: Marker>(&self, m: M) -> Option<&M::Type> where
AlignedSlice<A8>: AsAligned<<M::Type as AlignOf>::AlignOf>,
Get the value from the variant, if it matches the type passed in.
Example:
let a = v.get(gv!("ai"));
// a now has type &[i32]
sourcepub fn split(&self) -> (&[u8], &AlignedSlice<A8>)
pub fn split(&self) -> (&[u8], &AlignedSlice<A8>)
Destructures the variant into (typestr, data).
Note: typestr is not guaranteed to be a valid GVariant type.
Example use:
match v.split() {
(b"(is)", _) => {
let s = v.get(gv!("(is)"));
// Do something with s
}
(ty, _) => panic!("Unexpected variant type {:?}", ty)
}
Trait Implementations
sourceimpl Cast for Variant
impl Cast for Variant
sourcefn default_ref() -> &'static Self
fn default_ref() -> &'static Self
Get a static reference to the default value for this type. Read more
fn try_from_aligned_slice(
slice: &AlignedSlice<Self::AlignOf>
) -> Result<&Self, WrongSize>
fn try_from_aligned_slice_mut(
slice: &mut AlignedSlice<Self::AlignOf>
) -> Result<&mut Self, WrongSize>
sourcefn from_aligned_slice(slice: &AlignedSlice<Self::AlignOf>) -> &Self
fn from_aligned_slice(slice: &AlignedSlice<Self::AlignOf>) -> &Self
Cast slice
to type Self
. Read more