Skip to main content

LazyVec

Struct LazyVec 

Source
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>

Source

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:

  • As does not match the vector’s recorded primitive element type; or
  • the size of As does 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]);
Source

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.

Source

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.

Source

pub const fn len(&self) -> usize

Returns the number of elements in the vector.

Source

pub fn is_empty(&self) -> bool

Returns true if the vector contains no elements.

Source

pub const fn ty(&self) -> PrimitiveTy

Returns the vector’s primitive element type.

Trait Implementations§

Source§

impl<'a> Clone for LazyVec<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for LazyVec<'a>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'a> PartialEq for LazyVec<'a>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<'a> StructuralPartialEq for LazyVec<'a>

Auto Trait Implementations§

§

impl<'a> Forget for LazyVec<'a>

§

impl<'a> Freeze for LazyVec<'a>

§

impl<'a> Move for LazyVec<'a>

§

impl<'a> RefUnwindSafe for LazyVec<'a>

§

impl<'a> Send for LazyVec<'a>

§

impl<'a> Sync for LazyVec<'a>

§

impl<'a> Unpin for LazyVec<'a>

§

impl<'a> UnsafeUnpin for LazyVec<'a>

§

impl<'a> UnwindSafe for LazyVec<'a>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.