[][src]Struct gvariant::VariantWrap

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

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}.

Trait Implementations

impl<M: Clone + Marker, T: Clone + SerializeTo<M::Type>> Clone for VariantWrap<M, T>[src]

impl<M: Copy + Marker, T: Copy + SerializeTo<M::Type>> Copy for VariantWrap<M, T>[src]

impl<M: Debug + Marker, T: Debug + SerializeTo<M::Type>> Debug for VariantWrap<M, T>[src]

impl<M: Marker, T: SerializeTo<M::Type>> SerializeTo<Variant> for VariantWrap<M, T>[src]

Auto Trait Implementations

impl<M, T> RefUnwindSafe for VariantWrap<M, T> where
    M: RefUnwindSafe,
    T: RefUnwindSafe

impl<M, T> Send for VariantWrap<M, T> where
    M: Send,
    T: Send

impl<M, T> Sync for VariantWrap<M, T> where
    M: Sync,
    T: Sync

impl<M, T> Unpin for VariantWrap<M, T> where
    M: Unpin,
    T: Unpin

impl<M, T> UnwindSafe for VariantWrap<M, T> where
    M: UnwindSafe,
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.