pub struct Lazy<T: Model> { /* private fields */ }Expand description
A lazily-loaded related object that requires explicit load() call.
Unlike Related<T> which is loaded during the query via JOIN, Lazy<T>
defers loading until explicitly requested with a Session reference.
§States
- Empty: No FK value (null relationship)
- Unloaded: Has FK but not fetched yet
- Loaded: Object fetched and cached
§Example
ⓘ
// Field definition
struct Hero {
team: Lazy<Team>,
}
// Loading (requires Session)
let team = hero.team.load(&mut session, &cx).await?;
// After loading, access is fast
if let Some(team) = hero.team.get() {
println!("Team: {}", team.name);
}§N+1 Prevention
Use Session::load_many() to batch-load lazy relationships:
ⓘ
// Load all teams in one query
session.load_many(&mut heroes, |h| &mut h.team).await?;Implementations§
Source§impl<T: Model> Lazy<T>
impl<T: Model> Lazy<T>
Sourcepub async fn load<L>(
&mut self,
cx: &Cx,
loader: &mut L,
) -> Outcome<Option<&T>, Error>where
L: LazyLoader<T> + ?Sized,
pub async fn load<L>(
&mut self,
cx: &Cx,
loader: &mut L,
) -> Outcome<Option<&T>, Error>where
L: LazyLoader<T> + ?Sized,
Load the related object via the provided loader (cached after first success).
- If the FK is NULL, this caches
Noneand returnsOk(None). - If the loader errors/cancels/panics, this does not mark the relationship as loaded, allowing retries.
Trait Implementations§
Source§impl<'de, T> Deserialize<'de> for Lazy<T>where
T: Model + Deserialize<'de>,
impl<'de, T> Deserialize<'de> for Lazy<T>where
T: Model + Deserialize<'de>,
Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl<T> !Freeze for Lazy<T>
impl<T> RefUnwindSafe for Lazy<T>where
T: RefUnwindSafe + UnwindSafe,
impl<T> Send for Lazy<T>
impl<T> Sync for Lazy<T>
impl<T> Unpin for Lazy<T>where
T: Unpin,
impl<T> UnwindSafe for Lazy<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, _span: NoopSpan) -> Self
fn instrument(self, _span: NoopSpan) -> Self
Instruments this future with a span (no-op when disabled).
Source§fn in_current_span(self) -> Self
fn in_current_span(self) -> Self
Instruments this future with the current span (no-op when disabled).
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> ModelDump for Twhere
T: Serialize,
impl<T> ModelDump for Twhere
T: Serialize,
Source§fn model_dump(&self, options: DumpOptions) -> Result<Value, Error>
fn model_dump(&self, options: DumpOptions) -> Result<Value, Error>
Serialize a model to a JSON value. Read more
Source§fn model_dump_json(&self) -> Result<String, Error>
fn model_dump_json(&self) -> Result<String, Error>
Serialize a model to a JSON string with default options.
Source§fn model_dump_json_pretty(&self) -> Result<String, Error>
fn model_dump_json_pretty(&self) -> Result<String, Error>
Serialize a model to a pretty-printed JSON string.
Source§fn model_dump_json_with_options(
&self,
options: DumpOptions,
) -> Result<String, Error>
fn model_dump_json_with_options( &self, options: DumpOptions, ) -> Result<String, Error>
Serialize a model to a JSON string with full options support. Read more
Source§impl<T> ModelValidate for Twhere
T: DeserializeOwned,
impl<T> ModelValidate for Twhere
T: DeserializeOwned,
Source§fn model_validate(
input: impl Into<ValidateInput>,
options: ValidateOptions,
) -> Result<T, ValidationError>
fn model_validate( input: impl Into<ValidateInput>, options: ValidateOptions, ) -> Result<T, ValidationError>
Create and validate a model from input. Read more
Source§fn model_validate_json(json: &str) -> ValidateResult<Self>
fn model_validate_json(json: &str) -> ValidateResult<Self>
Create and validate a model from JSON string with default options.
Source§fn model_validate_dict(dict: HashMap<String, Value>) -> ValidateResult<Self>
fn model_validate_dict(dict: HashMap<String, Value>) -> ValidateResult<Self>
Create and validate a model from a HashMap with default options.