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
impl LazyRecord
Sourcepub fn from_raw(bytes: Bytes, format: Format) -> Self
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().
Sourcepub fn from_parsed(value: Value) -> Self
pub fn from_parsed(value: Value) -> Self
Create a lazy record from a parsed value.
The value is immediately available; bytes() will return None.
Sourcepub fn from_both(bytes: Bytes, format: Format, value: Value) -> Self
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.
Sourcepub fn value(&self, codec: &dyn Codec) -> Result<&Value, Error>
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.
Sourcepub fn value_if_parsed(&self) -> Option<&Value>
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.
Sourcepub fn bytes(&self) -> Option<&Bytes>
pub fn bytes(&self) -> Option<&Bytes>
Get the raw bytes if available.
Returns None if the record was created from a parsed value only.
Sourcepub fn format(&self) -> Option<&Format>
pub fn format(&self) -> Option<&Format>
Get the format hint if available.
Returns None if the record was created from a parsed value only.
Sourcepub fn into_record(self) -> Record
pub fn into_record(self) -> Record
Convert to a Record, consuming this lazy record.
If parsed, returns Record::Parsed. Otherwise returns Record::Raw.