Struct alkahest::Lazy

source ·
pub struct Lazy<'de, F: ?Sized> { /* private fields */ }
Expand description

Wrapper for lazy deserialization.

Lazy<F> deserializes data from formula F.

It can be used to deserialize value from formula F on demand. Also allows to deserialize that one value in place.

If generic parameter is slice (Lazy<[F]>) a lazy iterator can be obtained with Lazy::iter and Lazy::sized_iter(requires sized formula) methods.

If value is never needed, consider using Skip instead.

Implementations§

source§

impl<'de, F> Lazy<'de, F>where F: BareFormula + ?Sized,

source

pub fn get<T>(&self) -> Result<T, DeserializeError>where T: Deserialize<'de, F>,

Deserialize the lazy value.

Errors

Returns DeserializeError if deserialization fails.

source

pub fn get_in_place<T>(&self, place: &mut T) -> Result<(), DeserializeError>where T: Deserialize<'de, F> + ?Sized,

Deserialize the lazy value in place.

Errors

Returns DeserializeError if deserialization fails.

source§

impl<'de, F> Lazy<'de, [F]>where F: Formula,

source

pub fn sized_iter<T>(&self) -> DeIter<'de, F, T, IterSized> where T: Deserialize<'de, F>,

Produce iterator over lazy deserialized values.

Example
let mut buffer = [0u8; 1024];

serialize::<[u32], _>([1u8, 2, 3], &mut buffer).unwrap();
let (lazy, _) = deserialize::<[u32], Lazy<[u32]>>(&buffer).unwrap();
let mut iter = lazy.sized_iter::<u32>();
assert_eq!(iter.next().unwrap().unwrap(), 1);
assert_eq!(iter.next().unwrap().unwrap(), 2);
assert_eq!(iter.next().unwrap().unwrap(), 3);
assert!(iter.next().is_none());

sized_iter cannot be used to deserialize slice of unsized formulas. Attempt to use unsized formula will result in compile error.

let mut buffer = [0u8; 1024];

serialize::<[As<str>], _>(["qwe", "rty"], &mut buffer).unwrap();
let (lazy, _) = deserialize::<[As<str>], Lazy<[As<str>]>>(&buffer).unwrap();
let mut iter = lazy.sized_iter::<&str>();
assert_eq!(iter.next().unwrap().unwrap(), "qwe");
assert_eq!(iter.next().unwrap().unwrap(), "rty");
assert!(iter.next().is_none());
source

pub fn iter<T>(&self) -> DeIter<'de, F, T> where T: Deserialize<'de, F>,

Produce iterator over lazy deserialized values.

Example
let mut buffer = [0u8; 1024];

serialize::<[u32], _>([1u8, 2, 3], &mut buffer).unwrap();
let (lazy, _) = deserialize::<[u32], Lazy<[u32]>>(&buffer).unwrap();
let mut iter = lazy.iter::<u32>();
assert_eq!(iter.next().unwrap().unwrap(), 1);
assert_eq!(iter.next().unwrap().unwrap(), 2);
assert_eq!(iter.next().unwrap().unwrap(), 3);
assert!(iter.next().is_none());

iter may be used to deserialize slice of unsized formulas.

let mut buffer = [0u8; 1024];

serialize::<[As<str>], _>(["qwe", "rty"], &mut buffer).unwrap();
let (seq, _) = deserialize::<[As<str>], Lazy<[As<str>]>>(&buffer).unwrap();
let mut iter = seq.iter::<&str>();
assert_eq!(iter.next().unwrap().unwrap(), "qwe");
assert_eq!(iter.next().unwrap().unwrap(), "rty");
assert!(iter.next().is_none());

Trait Implementations§

source§

impl<'de, F: Clone + ?Sized> Clone for Lazy<'de, F>

source§

fn clone(&self) -> Lazy<'de, F>

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<'de, F> Debug for Lazy<'de, F>where F: ?Sized,

source§

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

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

impl<'de, 'fe: 'de, F> Deserialize<'fe, F> for Lazy<'de, F>where F: BareFormula + ?Sized,

source§

fn deserialize(de: Deserializer<'fe>) -> Result<Self, DeserializeError>

Deserializes value provided deserializer. Returns deserialized value and the number of bytes consumed from the and of input. Read more
source§

fn deserialize_in_place( &mut self, de: Deserializer<'fe> ) -> Result<(), DeserializeError>

Deserializes value in-place provided deserializer. Overwrites self with data from the input. Read more

Auto Trait Implementations§

§

impl<'de, F: ?Sized> RefUnwindSafe for Lazy<'de, F>

§

impl<'de, F: ?Sized> Send for Lazy<'de, F>

§

impl<'de, F: ?Sized> Sync for Lazy<'de, F>

§

impl<'de, F: ?Sized> Unpin for Lazy<'de, F>

§

impl<'de, F: ?Sized> UnwindSafe for Lazy<'de, F>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.