Struct gvariant::VariantWrap
source · [−]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: M
1: T
Trait Implementations
sourceimpl<M: Clone + Marker, T: Clone + SerializeTo<M::Type>> Clone for VariantWrap<M, T>
impl<M: Clone + Marker, T: Clone + SerializeTo<M::Type>> Clone for VariantWrap<M, T>
sourcefn clone(&self) -> VariantWrap<M, T>
fn clone(&self) -> VariantWrap<M, T>
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl<M: Debug + Marker, T: Debug + SerializeTo<M::Type>> Debug for VariantWrap<M, T>
impl<M: Debug + Marker, T: Debug + SerializeTo<M::Type>> Debug for VariantWrap<M, T>
sourceimpl<M: Marker, T: SerializeTo<M::Type>> SerializeTo<Variant> for VariantWrap<M, T>
impl<M: Marker, T: SerializeTo<M::Type>> SerializeTo<Variant> for VariantWrap<M, T>
impl<M: Copy + Marker, T: Copy + SerializeTo<M::Type>> Copy for VariantWrap<M, T>
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more