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
impl Decoder
Sourcepub fn new(schema: RootSchema) -> Self
pub fn new(schema: RootSchema) -> Self
Creates a decoder for schema.
Sourcepub const fn size(&self) -> Option<usize>
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.
Sourcepub fn fields<'a, 'de>(
&'a self,
reader: impl Reader<'de> + 'a,
) -> ReadResult<impl Iterator<Item = ReadResult<Field<'a, 'de>>> + 'a>
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());