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.
Implementations§
Trait Implementations§
Source§impl<T: Read + EncodeSize> EncodeSize for Lazy<T>
impl<T: Read + EncodeSize> EncodeSize for Lazy<T>
Source§fn encode_size(&self) -> usize
fn encode_size(&self) -> usize
Source§fn encode_inline_size(&self) -> usize
fn encode_inline_size(&self) -> usize
BufsMut::push
during Write::write_bufs. Used to size the working buffer for inline
writes. Override alongside Write::write_bufs for types where large
Bytes fields go via push; failing to do so will over-allocate.Source§impl<T: Read + Ord> Ord for Lazy<T>
impl<T: Read + Ord> Ord for Lazy<T>
Source§impl<T: Read + PartialOrd> PartialOrd for Lazy<T>
impl<T: Read + PartialOrd> PartialOrd for Lazy<T>
Source§impl<T: Read + FixedSize> Read for Lazy<T>
impl<T: Read + FixedSize> Read for Lazy<T>
Source§impl<T: Read + Write> Write for Lazy<T>
impl<T: Read + Write> Write for Lazy<T>
Source§fn write_bufs(&self, buf: &mut impl BufsMut)
fn write_bufs(&self, buf: &mut impl BufsMut)
BufsMut, allowing existing Bytes chunks to be
appended via BufsMut::push instead of written inline. Must encode
to the same format as Write::write. Defaults to Write::write.