Skip to main content

Lazy

Struct Lazy 

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

Source§

impl<T: Read> Lazy<T>

Source

pub fn new(value: T) -> Self

Create a Lazy using a value.

Source

pub fn deferred(buf: &mut impl Buf, cfg: T::Cfg) -> Self

Create a Lazy by deferring decoding of an underlying value.

The only cost incurred when this function is called is that of copying some bytes.

Use Self::get to access the actual value, by decoding these bytes.

Source§

impl<T: Read> Lazy<T>

Source

pub fn get(&self) -> Option<&T>

Force decoding of the underlying value.

This will return None only if decoding the value fails.

This function wil incur the cost of decoding the value only once, so there’s no need to cache its output.

Trait Implementations§

Source§

impl<T: Clone + Read> Clone for Lazy<T>

Source§

fn clone(&self) -> Lazy<T>

Returns a duplicate 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<T: Read + Debug> Debug for Lazy<T>

Source§

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

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

impl<T: Read + EncodeSize> EncodeSize for Lazy<T>

Source§

fn encode_size(&self) -> usize

Returns the encoded size of this value (in bytes).
Source§

impl<T: Read + Encode> From<T> for Lazy<T>

Source§

fn from(value: T) -> Self

Converts to this type from the input type.
Source§

impl<T: Read + Hash> Hash for Lazy<T>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: Read + Ord> Ord for Lazy<T>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: Read + PartialEq> PartialEq for Lazy<T>

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Read + PartialOrd> PartialOrd for Lazy<T>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Read + FixedSize> Read for Lazy<T>

Source§

type Cfg = <T as Read>::Cfg

The Cfg type parameter allows passing configuration during the read process. This is crucial for safely decoding untrusted data, for example, by providing size limits for collections or strings. Read more
Source§

fn read_cfg(buf: &mut impl Buf, cfg: &Self::Cfg) -> Result<Self, Error>

Reads a value from the buffer using the provided configuration cfg. Read more
Source§

impl<T: Read + Write> Write for Lazy<T>

Source§

fn write(&self, buf: &mut impl BufMut)

Writes the binary representation of self to the provided buffer buf. Read more
Source§

impl<T: Read + Eq> Eq for Lazy<T>

Auto Trait Implementations§

§

impl<T> !Freeze for Lazy<T>

§

impl<T> RefUnwindSafe for Lazy<T>

§

impl<T> Send for Lazy<T>
where T: Send,

§

impl<T> Sync for Lazy<T>
where T: Sync + Send,

§

impl<T> Unpin for Lazy<T>
where <T as Read>::Cfg: Unpin, T: Unpin,

§

impl<T> UnwindSafe for Lazy<T>
where <T as Read>::Cfg: UnwindSafe, T: UnwindSafe,

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> Decode for T
where T: Read,

Source§

fn decode_cfg(buf: impl Buf, cfg: &Self::Cfg) -> Result<Self, Error>

Decodes a value from buf using cfg, ensuring the entire buffer is consumed. Read more
Source§

impl<X, T> DecodeExt<X> for T
where X: IsUnit, T: Decode<Cfg = X>,

Source§

fn decode(buf: impl Buf) -> Result<Self, Error>

Decodes a value using the default () config.
Source§

impl<X, U> DecodeRangeExt<X> for U
where X: IsUnit, U: Decode<Cfg = (RangeCfg<usize>, X)>,

Source§

fn decode_range( buf: impl Buf, range: impl RangeBounds<usize>, ) -> Result<Self, Error>

Decodes a value using only a range configuration. Read more
Source§

impl<T> Encode for T
where T: Write + EncodeSize,

Source§

fn encode(&self) -> Bytes

Encodes self into a new Bytes buffer. Read more
Source§

fn encode_mut(&self) -> BytesMut

Encodes self into a new BytesMut buffer. 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> ReadExt for T
where T: Read<Cfg = ()>,

Source§

fn read(buf: &mut impl Buf) -> Result<Self, Error>

Reads a value using the default () config.
Source§

impl<X, U> ReadRangeExt<X> for U
where X: IsUnit, U: Read<Cfg = (RangeCfg<usize>, X)>,

Source§

fn read_range( buf: &mut impl Buf, range: impl RangeBounds<usize>, ) -> Result<Self, Error>

Reads a value using only a range configuration. Read more
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 = Infallible

The type returned in the event of a conversion error.
Source§

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

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

impl<T> Codec for T
where T: Encode + Decode,

Source§

impl<T> CodecShared for T
where T: Codec + Send + Sync,

Source§

impl<T> EncodeShared for T
where T: Encode + Send + Sync,