pub struct VariantWrap<M: Marker, T: SerializeTo<M::Type>>(pub M, pub T);
Expand description

Mark a value to be serialised as type v

GVariant has a variant type which can contain a value of any GVariant type. This can be used to implment enums. This is a wrapper type that helps serialising to those types.

For example: Instead of serialising this as type i:

use gvariant::{gv, Marker, VariantWrap};
let x: i32 = 5;
let serialized_i = gv!("i").serialize_to_vec(x);

This code will serialised to a value of type v that contains a value of type i:

let serialized_vi = gv!("v").serialize_to_vec(VariantWrap(gv!("i"), x));

Similarly you can wrap an i in a v in another v:

let serialized_vvi = gv!("v").serialize_to_vec(
    VariantWrap(gv!("v"), VariantWrap(gv!("i"), x)));

Typically you’d represent rust enums as GVariant variants. The best way to serialize enums as variants is to implement SerializeTo for the enum. Example:

enum MyEnum {
    Bool(bool),
    String(String),
}
impl SerializeTo<gvariant::Variant> for &MyEnum {
    fn serialize(self, f: &mut impl std::io::Write) -> std::io::Result<usize> {
        match self {
            MyEnum::Bool(x) => VariantWrap(gv!("b"), x).serialize(f),
            MyEnum::String(x) => VariantWrap(gv!("s"), x).serialize(f),
        }
    }
}

A common type type seen in the wild is the “bag of properties” a{sv}.

Tuple Fields

0: M1: T

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.