[−][src]Trait gvariant::Marker
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.
Associated Types
type Type: Cast + ?Sized
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.
Associated Constants
const TYPESTR: &'static [u8]
The typestr that was passed to the gv!
macro.
Provided methods
fn cast<'a>(
&self,
data: &'a AlignedSlice<<Self::Type as AlignOf>::AlignOf>
) -> &'a Self::Type
&self,
data: &'a AlignedSlice<<Self::Type as AlignOf>::AlignOf>
) -> &'a Self::Type
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();
fn try_cast_mut<'a>(
data: &'a mut AlignedSlice<<Self::Type as AlignOf>::AlignOf>
) -> Result<&'a mut Self::Type, WrongSize>
data: &'a mut AlignedSlice<<Self::Type as AlignOf>::AlignOf>
) -> Result<&'a mut Self::Type, WrongSize>
Cast data
to the appropriate rust type Self::Type
for the type
string Self::TYPESTR
.
fn deserialize(&self, r: impl Read) -> Result<<Self::Type as ToOwned>::Owned>
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.
fn from_bytes(&self, data: impl AsRef<[u8]>) -> <Self::Type as ToOwned>::Owned
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 alloc_aligned
or read_to_slice
, 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");
fn serialize(
&self,
data: impl SerializeTo<Self::Type>,
out: &mut impl Write
) -> Result<usize>
&self,
data: impl SerializeTo<Self::Type>,
out: &mut impl Write
) -> Result<usize>
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)?;
fn serialize_to_vec(&self, data: impl SerializeTo<Self::Type>) -> Vec<u8>
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.