Skip to main content

Decoder

Enum Decoder 

Source
pub enum Decoder<'schema> {
    Struct(SchemaDecoder<'schema>),
    Enum(EnumDecoder<'schema>),
}
Expand description

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

A decoder borrows its RootSchema and can be reused for every value encoded with that schema. Create one with RootSchema::decoder or Decoder::new.

Call Decoder::fields to iterate lazily over a payload’s fields for either a struct or an enum. For enums, this reads the variant tag and selects its fields automatically.

Match on the decoder when the distinction between structs and enums matters:

In every path, individual fields are decoded as the iterator advances, and field decoding errors are returned by the corresponding iterator item.

§Examples

Inspect an enum variant before decoding its fields using the same reader:

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

#[derive(SchemaDynamic, SchemaRead, SchemaWrite)]
enum Message {
    Ping,
    Value(u64),
}

let schema = Message::schema();
let Decoder::Enum(decoder) = schema.decoder() else {
    panic!("expected an enum schema");
};

let encoded = wincode::serialize(&Message::Value(42))?;
let mut reader = &encoded[..];
let variant = decoder.decode_variant(reader)?;
let name = variant.variant_name();
let mut fields = variant.fields();
assert_eq!(name, "Value");
assert_eq!(fields.next().expect("value field")?.value(), &Value::U64(42));
assert!(fields.next().is_none());

Variants§

§

Struct(SchemaDecoder<'schema>)

A decoder for struct payloads.

§

Enum(EnumDecoder<'schema>)

A decoder for selecting and decoding enum variants.

Implementations§

Source§

impl<'schema> Decoder<'schema>

Source

pub fn new(schema: &'schema RootSchema) -> Self

Creates a decoder for encoded values matching the borrowed schema.

Source

pub fn fields<'de, R: Reader<'de>>( &self, reader: R, ) -> ReadResult<Fields<'schema, 'de, R>>

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. Dropping the iterator early leaves the remaining fields unread.

§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<'schema> Debug for Decoder<'schema>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'schema> Freeze for Decoder<'schema>

§

impl<'schema> RefUnwindSafe for Decoder<'schema>

§

impl<'schema> Send for Decoder<'schema>

§

impl<'schema> Sync for Decoder<'schema>

§

impl<'schema> Unpin for Decoder<'schema>

§

impl<'schema> UnsafeUnpin for Decoder<'schema>

§

impl<'schema> UnwindSafe for Decoder<'schema>

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.