Skip to main content

LazyRecord

Struct LazyRecord 

Source
pub struct LazyRecord { /* private fields */ }
Expand description

A record with lazy parsing. Thread-safe.

Unlike Record, which is either raw or parsed, LazyRecord can be both simultaneously. It caches the parsed result on first access.

§Use Cases

  • Middleware that might need to inspect data based on some condition
  • Caching layers that want to keep both representations
  • Any scenario where parsing cost should be deferred and amortized

§Example

use structfs_core_store::{LazyRecord, Format, Value};
use bytes::Bytes;

let record = LazyRecord::from_raw(
    Bytes::from_static(b"{\"name\":\"Alice\"}"),
    Format::JSON,
);

// No parsing yet
assert!(record.bytes().is_some());

// Now parse (would require a codec in real use)
// let value = record.value(&codec)?;

§Thread Safety

LazyRecord is Send + Sync. Multiple threads can safely call value() concurrently - only one will actually parse, others will wait and get the cached result.

Implementations§

Source§

impl LazyRecord

Source

pub fn from_raw(bytes: Bytes, format: Format) -> Self

Create a lazy record from raw bytes.

The bytes will be parsed on first call to value().

Source

pub fn from_parsed(value: Value) -> Self

Create a lazy record from a parsed value.

The value is immediately available; bytes() will return None.

Source

pub fn from_both(bytes: Bytes, format: Format, value: Value) -> Self

Create a lazy record with both raw bytes and parsed value.

Useful when you’ve already parsed and want to cache both.

Source

pub fn value(&self, codec: &dyn Codec) -> Result<&Value, Error>

Get the value, parsing if necessary.

This method is thread-safe. If multiple threads call it concurrently, only one will actually parse; others will wait and get the cached result.

§Errors

Returns an error if:

  • The record was created from parsed value only and bytes are needed (won’t happen)
  • The codec fails to parse the bytes
§Panics

This method will panic if called on a LazyRecord with no raw data and the value hasn’t been set. This should never happen with proper construction.

Source

pub fn value_if_parsed(&self) -> Option<&Value>

Get the value if already parsed, without triggering parsing.

Returns None if the value hasn’t been parsed yet.

Source

pub fn bytes(&self) -> Option<&Bytes>

Get the raw bytes if available.

Returns None if the record was created from a parsed value only.

Source

pub fn format(&self) -> Option<&Format>

Get the format hint if available.

Returns None if the record was created from a parsed value only.

Source

pub fn is_parsed(&self) -> bool

Check if the value has been parsed.

Source

pub fn has_bytes(&self) -> bool

Check if raw bytes are available.

Source

pub fn into_record(self) -> Record

Convert to a Record, consuming this lazy record.

If parsed, returns Record::Parsed. Otherwise returns Record::Raw.

Trait Implementations§

Source§

impl Clone for LazyRecord

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for LazyRecord

Source§

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

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

impl From<Record> for LazyRecord

Source§

fn from(record: Record) -> Self

Converts to this type from the input type.
Source§

impl Send for LazyRecord

Source§

impl Sync for LazyRecord

Auto Trait Implementations§

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> 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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.