pub struct LazyVec<'a> { /* private fields */ }Expand description
A lazily decoded vector of fixed-width primitive values.
The encoded payload remains borrowed when the input reader supports stable
borrowing. Use Self::try_iter_as to iterate by reference when the
concrete element type is known, Self::try_into_iter_as to create an
owning iterator, or Self::into_dyn_vec to decode dynamically typed
values.
Implementations§
Source§impl<'a> LazyVec<'a>
impl<'a> LazyVec<'a>
Sourcepub fn try_iter_as<'iter, As>(&'iter self) -> ReadResult<Iter<'iter, As>>where
As: DynPrimitiveTy,
pub fn try_iter_as<'iter, As>(&'iter self) -> ReadResult<Iter<'iter, As>>where
As: DynPrimitiveTy,
Returns a borrowing, lazy iterator of As values.
The requested type must match the primitive element type recorded in
the schema. This prevents, for example, interpreting the payload of
a Vec<u64> as a sequence of u8 values.
This method validates the element type and its width, but it does
not decode any elements. Decoding happens as the returned iterator
is advanced, and each item is returned as a ReadResult.
Consequently, malformed element encodings are reported by
the iterator item that encounters them rather than by this
method.
The iterator borrows this lazy vector. Multiple iterators can be created without cloning or decoding the payload.
To iterate, As must also implement
SchemaRead<DefaultConfig, Dst = As>.
§Errors
Returns an error if:
Asdoes not match the vector’s recorded primitive element type; or- the size of
Asdoes not match the recorded element width.
§Examples
use wincode::{ReadResult, SchemaRead, SchemaWrite};
use wincode_dynamic::{Decoder, SchemaDynamic, Value};
#[derive(SchemaDynamic, SchemaRead, SchemaWrite)]
struct Message {
values: Vec<u64>,
}
let encoded = wincode::serialize(&Message {
values: vec![10, 20, 30],
})
.expect("serialize message");
let schema = Message::schema();
let decoder = Decoder::new(&schema);
let mut fields = decoder.fields(encoded.as_slice())?;
let field = fields.next().expect("values field")?;
assert_eq!(field.name(), "values");
let Value::Vec(values) = field.value() else {
panic!("expected a vector");
};
let values = values
.try_iter_as::<u64>()?
.collect::<ReadResult<Vec<_>>>()?;
assert_eq!(values, [10, 20, 30]);Sourcepub fn try_into_iter_as<As>(self) -> ReadResult<IntoIter<'a, As>>where
As: DynPrimitiveTy,
pub fn try_into_iter_as<As>(self) -> ReadResult<IntoIter<'a, As>>where
As: DynPrimitiveTy,
Converts this vector into an owning, lazy iterator of As values.
This performs the same type validation as Self::try_iter_as, but
consumes the lazy vector and moves its payload into the iterator.
Borrowed payloads remain borrowed, and owned payloads are moved
without cloning or reallocating them.
§Errors
Returns an error if As does not match the vector’s recorded
primitive element type or width.
Sourcepub fn into_dyn_vec(self) -> ReadResult<Vec<PrimitiveValue>>
pub fn into_dyn_vec(self) -> ReadResult<Vec<PrimitiveValue>>
Converts this lazy vector into a Vec<PrimitiveValue> by decoding
all elements using the vector’s recorded primitive element type.
§Errors
Returns the first element decoding error encountered in the payload.
Sourcepub const fn ty(&self) -> PrimitiveTy
pub const fn ty(&self) -> PrimitiveTy
Returns the vector’s primitive element type.