pub trait Marker: Copy {
    type Type: Cast + ?Sized;

    const TYPESTR: &'static [u8];

    fn cast<'a>(
        &self,
        data: &'a AlignedSlice<<Self::Type as AlignOf>::AlignOf>
    ) -> &'a Self::Type { ... } fn try_cast_mut(
        data: &mut AlignedSlice<<Self::Type as AlignOf>::AlignOf>
    ) -> Result<&mut Self::Type, WrongSize> { ... } fn deserialize(
        &self,
        r: impl Read
    ) -> Result<<Self::Type as ToOwned>::Owned> { ... } fn from_bytes(
        &self,
        data: impl AsRef<[u8]>
    ) -> <Self::Type as ToOwned>::Owned { ... } fn serialize(
        &self,
        data: impl SerializeTo<Self::Type>,
        out: &mut impl Write
    ) -> Result<usize> { ... } fn serialize_to_vec(&self, data: impl SerializeTo<Self::Type>) -> Vec<u8> { ... } }
Expand description

This is the return type of the gv! macro.

This acts as a kind of factory trait for GVariant types, creating them from aligned data using the cast method.

Do not implement this trait yourself, gv! is responsible for creating the marker structs that implement this. Use that instead.

See the documentation of gv! for usage examples.

Required Associated Types

This type has the same representation in memory as the GVariant type with the signature given by Self::TYPESTR, and implements Cast so it can be created from appropriately aligned data.

Required Associated Constants

The typestr that was passed to the gv! macro.

Provided Methods

Cast data to the appropriate rust type Self::Type for the type string Self::TYPESTR.

Use this in preference to deserialize if you already have the data in (properly aligned) memory. This makes no allocations and has no dependency on std or alloc.

Example

let (my_int, my_str) = gv!("(ias)").cast(aligned_data).to_tuple();

Cast data to the appropriate rust type Self::Type for the type string Self::TYPESTR.

Read the data from r returning an owned deserialised GVariant object

Example

let v = gv!("s").deserialize(std::fs::File::open(myfile)?)?;
assert_eq!(&*v, "An example string");

This requires the features std and alloc be enabled on the gvariant crate.

Deserialise the given data, making a copy in the process.

This is a convenience API wrapper around copy_to_align and cast allowing users to not have to think about the alignment of their data. It is usually better to ensure the data you have is aligned, for example using copy_to_align or AlignedBuf, and then use cast directly. This way you can avoid additional allocations, avoid additional copying, and work in noalloc contexts.

Example

let v = gv!("s").from_bytes(b"An example string\0");
assert_eq!(&*v, "An example string");

Serialize the data to the given stream as a GVariant

To be serialized as a GVariant the passed type must implement SerializeTo<> for the relevant GVariant type.

Example

let comment = Some("It's great!");
gv!("ms").serialize(&comment, &mut myfile)?;

For information on how to serialize to the variant type see VariantWrap.

Convenience method for in-memory serialization

Used by our tests. You probably want to use the more flexible serialize instead which can be used to write to files/sockets.

Implementors