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:
Decoder::Structexposes aSchemaDecoder. Itsfieldsmethod creates the iterator infallibly, since there is no variant tag to read.Decoder::Enumexposes anEnumDecoder. Calldecode_variantto read and validate the variant tag, returning aVariantDecoder. Itsvariant_nameis always available, including for unit variants. Then consume it withfieldsto iterate over the selected fields. The tag is read once; field iteration continues from the reader’s resulting position.
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>
impl<'schema> Decoder<'schema>
Sourcepub fn new(schema: &'schema RootSchema) -> Self
pub fn new(schema: &'schema RootSchema) -> Self
Creates a decoder for encoded values matching the borrowed schema.
Sourcepub fn fields<'de, R: Reader<'de>>(
&self,
reader: R,
) -> ReadResult<Fields<'schema, 'de, R>>
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());