[−][src]Struct gvariant::VariantWrap
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]
fn clone(&self) -> VariantWrap<M, T>
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[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,
M: RefUnwindSafe,
T: RefUnwindSafe,
impl<M, T> Send for VariantWrap<M, T> where
M: Send,
T: Send,
M: Send,
T: Send,
impl<M, T> Sync for VariantWrap<M, T> where
M: Sync,
T: Sync,
M: Sync,
T: Sync,
impl<M, T> Unpin for VariantWrap<M, T> where
M: Unpin,
T: Unpin,
M: Unpin,
T: Unpin,
impl<M, T> UnwindSafe for VariantWrap<M, T> where
M: UnwindSafe,
T: UnwindSafe,
M: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,