pub struct Lazy<T> { /* private fields */ }
Expand description
A wrapper around a type that allows it to be lazily decoded.
This is useful for types that are expensive to decode, but allowed
to be skipped over by the spec (e.g. as part of a length-prefixed
Blob
). During decoding, this type will
store the raw bytes of the value, and only decode them when
explicitly requested.
When re-encoding, it will check if the value has ever been accessed mutably, and if so, re-encode it. Otherwise it will do a cheap copy of the original raw bytes.
Implementations§
source§impl<T> Lazy<T>
impl<T> Lazy<T>
sourcepub fn from_raw(raw: UnparsedBytes) -> Self
pub fn from_raw(raw: UnparsedBytes) -> Self
Create a new undecoded Lazy
from a raw byte vector.
sourcepub fn try_as_raw(&self) -> Result<&UnparsedBytes, &T>
pub fn try_as_raw(&self) -> Result<&UnparsedBytes, &T>
Retrieve the raw bytes if the value has not been modified yet.
source§impl<T: Decode> Lazy<T>
impl<T: Decode> Lazy<T>
sourcepub fn try_contents(&self) -> Result<&T, DecodeError>
pub fn try_contents(&self) -> Result<&T, DecodeError>
Retrieve a reference to the inner value, decoding it if it wasn’t already.
sourcepub fn try_contents_mut(&mut self) -> Result<&mut T, DecodeError>
pub fn try_contents_mut(&mut self) -> Result<&mut T, DecodeError>
Retrieve a mutable reference to the inner value, decoding it if it wasn’t already.
This will invalidate the original raw bytes.
sourcepub fn try_into_contents(self) -> Result<T, DecodeError>
pub fn try_into_contents(self) -> Result<T, DecodeError>
Unwrap the inner value, decoding it if it wasn’t already.