Skip to main content

Decoder

Struct Decoder 

Source
pub struct Decoder { /* private fields */ }
Expand description

Decodes a wincode-encoded payload reflectively using a runtime RootSchema.

A decoder only needs to be created once for a given RootSchema and can then be reused for every value encoded with that schema. Call Decoder::fields to iterate over a value’s decoded fields.

Implementations§

Source§

impl Decoder

Source

pub fn new(schema: RootSchema) -> Self

Creates a decoder for schema.

Source

pub fn name(&self) -> &str

Returns the root struct or enum name.

Source

pub const fn size(&self) -> Option<usize>

Returns the complete encoded size of a root value when it is fixed.

For enums, this includes the variant tag. The size uses wincode’s default configuration; None means it depends on the encoded value.

Source

pub fn fields<'a, 'de>( &'a self, reader: impl Reader<'de> + 'a, ) -> ReadResult<impl Iterator<Item = ReadResult<Field<'a, 'de>>> + 'a>

Returns a lazy iterator over the fields in an encoded payload.

Fields are decoded in schema order as the iterator advances. For an enum, this method first reads the variant tag and then iterates over that variant’s fields.

§Errors

Returns an error if an enum variant tag is invalid. Errors encountered while decoding a field are returned by the corresponding iterator item.

§Examples
use wincode::{SchemaRead, SchemaWrite};
use wincode_dynamic::{Decoder, RootSchema, SchemaDynamic, Value};

#[derive(SchemaDynamic, SchemaRead, SchemaWrite)]
struct Message {
    id: u64,
    active: bool,
}

let encoded_schema = wincode::serialize(&Message::schema())?;
let encoded_value = wincode::serialize(&Message {
    id: 42,
    active: true,
})?;

let schema = wincode::deserialize::<RootSchema>(&encoded_schema)?;
let decoder = Decoder::new(schema);
let mut fields = decoder.fields(&encoded_value[..])?;

let id = fields.next().expect("id field")?;
assert_eq!(id.name(), "id");
assert_eq!(id.value(), &Value::U64(42));

let active = fields.next().expect("active field")?;
assert_eq!(active.name(), "active");
assert_eq!(active.value(), &Value::Bool(true));
assert!(fields.next().is_none());

Trait Implementations§

Source§

impl Debug for Decoder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.