Skip to main content

Lazy

Struct Lazy 

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

Source

pub fn empty() -> Self

Create an empty lazy relationship (null FK).

Source

pub fn from_fk(fk: impl Into<Value>) -> Self

Create from a foreign key value (not yet loaded).

Source

pub fn loaded(obj: T) -> Self

Create with an already-loaded object.

Source

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 None and returns Ok(None).
  • If the loader errors/cancels/panics, this does not mark the relationship as loaded, allowing retries.
Source

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

Get the loaded object (None if not loaded or FK is null).

Source

pub fn is_loaded(&self) -> bool

Check if load() has been called.

Source

pub fn is_empty(&self) -> bool

Check if the relationship is empty (null FK).

Source

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

Get the foreign key value.

Source

pub fn set_loaded(&self, obj: Option<T>) -> Result<(), Option<T>>

Set the loaded object (internal use by Session::load_many).

Returns Ok(()) if successfully set, Err if already loaded.

Source

pub fn reset(&mut self)

Reset the lazy relationship to unloaded state.

This is useful when refreshing an object after commit.

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> Self

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: Model + 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: Model> Default for Lazy<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

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>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T> Serialize for Lazy<T>
where T: Model + Serialize,

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for Lazy<T>

§

impl<T> RefUnwindSafe for Lazy<T>

§

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> 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> Instrument for T

Source§

fn instrument(self, _span: NoopSpan) -> Self

Instruments this future with a span (no-op when disabled).
Source§

fn in_current_span(self) -> Self

Instruments this future with the current span (no-op when disabled).
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> ModelDump for T
where T: Serialize,

Source§

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>

Serialize a model to a JSON string with default options.
Source§

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>

Serialize a model to a JSON string with full options support. Read more
Source§

impl<T> ModelValidate for T

Source§

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>

Create and validate a model from JSON string with default options.
Source§

fn model_validate_dict(dict: HashMap<String, Value>) -> ValidateResult<Self>

Create and validate a model from a HashMap with default options.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,