pub struct Lazy<T: Read> { /* private fields */ }Expand description
A type which can be deserialized lazily.
This is useful when deserializing a value is expensive, and you don’t want to immediately pay this cost. This type allows you to move this cost to a later point in your program, or use parallelism to spread the cost across computing cores.
§Usage
Any usage of the type requires that T implements Read, because we need
to know what type Read::Cfg is.
§Construction
If you have a T, you can use Lazy::new:
let l = Lazy::new(4000u64);or Into:
let l: Lazy<u64> = 4000u64.into();If you don’t have a T, then you can instead create a Lazy using
bytes and a Read::Cfg:
let l: Lazy<u64> = Lazy::deferred(&mut 4000u64.encode(), ());§Consumption
Given a Lazy, use Lazy::get to access the value:
let l = Lazy::<u64>::deferred(&mut 4000u64.encode(), ());
assert_eq!(l.get(), Some(&4000u64));
// Does not pay the cost of deserializing again
assert_eq!(l.get(), Some(&4000u64));This returns an Option, because deserialization might fail.
§Traits
Lazy can be serialized and deserialized, implementing Read, Write,
and EncodeSize, based on the underlying implementation of T.
Furthermore, we implement Eq, Ord, Hash based on the implementation
of T as well. These methods will force deserialization of the value.