[−][src]Struct zvariant::Structure
An ordered collection of items of arbitrary types.
This is mostly just a way to support custom data structures. In the future, we'll hopefully
provide a way to easily map a Rust struct into a Structure
(probably through an attribute
macro).
Example
use core::convert::TryInto; use zvariant::{Array, Decode, Encode, EncodingFormat}; use zvariant::Structure; // Create a complex structure let top_struct = Structure::new() // top-most simple fields .add_field(u8::max_value()) .add_field(u32::max_value()) // top-most inner structure .add_field( Structure::new() // 2nd level simple fields .add_field(i64::max_value()) .add_field(true) .add_field(i64::max_value()) // 2nd level array field .add_field(Array::from(vec!["Hello", "World"])), ) // one more top-most simple field .add_field("hello"); assert!(top_struct.signature() == "(yu(xbxas)s)"); // Encode it let format = EncodingFormat::default(); let encoding = top_struct.encode(format); assert!(encoding.len() == 70); // Then decode it let top_struct = Structure::decode(encoding, top_struct.signature(), format).unwrap(); // Now check if everything is as expected let fields = top_struct.fields(); // top-most simple fields assert!(*u8::from_variant(&fields[0]).unwrap() == u8::max_value()); assert!(*u32::from_variant(&fields[1]).unwrap() == u32::max_value()); assert!(String::from_variant(&fields[3]).unwrap() == "hello"); // top-most inner structure let inner = Structure::from_variant(&fields[2]).unwrap(); let inner_fields = inner.fields(); // 2nd level simple fields assert!(*i64::from_variant(&inner_fields[0]).unwrap() == i64::max_value()); assert!(*bool::from_variant(&inner_fields[1]).unwrap() == true); // 2nd level array field let array = Array::from_variant(&inner_fields[3]).unwrap(); let as_: Vec<&String> = array.try_into().unwrap(); assert!(as_ == ["Hello", "World"]);
Methods
impl Structure
[src]
pub fn take_fields(self) -> Vec<Variant>
[src]
Get all the fields, consuming self
.
pub fn fields(&self) -> &[Variant]
[src]
Get a reference to all the fields of self
.
pub fn new() -> Self
[src]
Create a new Structure
.
Same as Structure::default()
.
pub fn add_field<T>(self, field: T) -> Self where
T: Encode,
[src]
T: Encode,
Append field
to self
.
This method returns Self
so that you can use the builder pattern to create a complex
structure.
Trait Implementations
impl Clone for Structure
[src]
impl Debug for Structure
[src]
impl Decode for Structure
[src]
fn slice_data(
data: impl Into<SharedData>,
signature: impl Into<Signature>,
format: EncodingFormat
) -> Result<SharedData, VariantError>
[src]
data: impl Into<SharedData>,
signature: impl Into<Signature>,
format: EncodingFormat
) -> Result<SharedData, VariantError>
fn decode(
data: impl Into<SharedData>,
signature: impl Into<Signature>,
format: EncodingFormat
) -> Result<Self, VariantError>
[src]
data: impl Into<SharedData>,
signature: impl Into<Signature>,
format: EncodingFormat
) -> Result<Self, VariantError>
fn ensure_correct_signature(
signature: impl Into<Signature>
) -> Result<Signature, VariantError>
[src]
signature: impl Into<Signature>
) -> Result<Signature, VariantError>
fn slice_signature(
signature: impl Into<Signature>
) -> Result<Signature, VariantError>
[src]
signature: impl Into<Signature>
) -> Result<Signature, VariantError>
fn take_from_variant(variant: Variant) -> Result<Self, VariantError>
[src]
fn from_variant(variant: &Variant) -> Result<&Self, VariantError>
[src]
fn slice_for_decoding(
data: impl Into<SharedData>,
signature: impl Into<Signature>,
format: EncodingFormat
) -> Result<SharedData, VariantError>
[src]
data: impl Into<SharedData>,
signature: impl Into<Signature>,
format: EncodingFormat
) -> Result<SharedData, VariantError>
impl Default for Structure
[src]
impl Encode for Structure
[src]
const SIGNATURE_CHAR: char
[src]
const SIGNATURE_STR: &'static str
[src]
const ALIGNMENT: usize
[src]
fn encode_into(&self, bytes: &mut Vec<u8>, format: EncodingFormat)
[src]
fn signature(&self) -> Signature
[src]
fn to_variant(self) -> Variant
[src]
fn is(variant: &Variant) -> bool
[src]
fn encode(&self, format: EncodingFormat) -> Vec<u8>
[src]
fn add_padding(bytes: &mut Vec<u8>, format: EncodingFormat)
[src]
fn padding(n_bytes_before: usize, _format: EncodingFormat) -> usize
[src]
Auto Trait Implementations
impl RefUnwindSafe for Structure
impl Send for Structure
impl Sync for Structure
impl Unpin for Structure
impl UnwindSafe for Structure
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>,