Skip to main content

Deferred

Struct Deferred 

Source
pub struct Deferred<T> { /* private fields */ }
Expand description

A field whose value is not loaded by default.

Deferred<T> wraps a T whose underlying column is excluded from default queries. After a normal load the value is unloaded and accessing it via get panics. Use .include() on the query to load the value.

Deferred<Option<T>> is supported when the column is nullable.

§Serde

With the serde feature a loaded Deferred<T> serializes transparently as its inner T, and any present value deserializes back to the loaded state. The unloaded state has no transparent encoding of its own — skip it on the way out and default it on the way in:

ⓘ
#[serde(skip_serializing_if = "Deferred::is_unloaded", default)]
notes: Deferred<Option<String>>,

Serializing an unloaded field without skip_serializing_if emits null, which does not round-trip (it reads back as loaded), so the annotation is expected on every deferred field.

Implementations§

Source§

impl<T> Deferred<T>

Source

pub fn is_unloaded(&self) -> bool

Returns true if the field has not been loaded.

Source

pub fn get(&self) -> &T

Returns a reference to the loaded value.

§Panics

Panics if the field has not been loaded.

Source

pub fn unload(&mut self)

Clears the loaded value, returning the field to the unloaded state.

Source

pub fn into_inner(self) -> T

Consumes this Deferred<T> and returns the loaded value.

§Panics

Panics if the field has not been loaded.

Trait Implementations§

Source§

impl<T> Clone for Deferred<T>
where T: Clone,

Source§

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

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<T> Debug for Deferred<T>
where T: Debug,

Source§

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

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

impl<T> Default for Deferred<T>

Source§

fn default() -> Deferred<T>

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

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

Available on crate feature serde only.

Deserializes any present value — including null — as loaded, mirroring the transparent Serialize impl above. An absent field is left unloaded, but only via #[serde(default)]; see the type-level docs.

Source§

fn deserialize<D>( deserializer: D, ) -> Result<Deferred<T>, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

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

impl<T> Field for Deferred<T>
where T: Field,

Source§

const NULLABLE: bool = T::NULLABLE

Whether or not the type is nullable
Source§

const DEFERRED: bool = true

Whether this field is omitted from default loads.
Source§

type ExprTarget = <T as Field>::ExprTarget

The expression-level type of this field. Read more
Source§

type Path<Origin> = <T as Field>::Path<Origin>

The type returned when accessing this field from a Fields struct. For primitives, this is Path<Origin, Self>. For embedded types, this is {Type}Fields.
Source§

type ListPath<Origin> = <T as Field>::ListPath<Origin>

The type returned when accessing this field from a list Fields struct. For primitives, this is Path<Origin, ListSelf::ExprTarget>. For embedded types, this is {Type}ListFields.
Source§

type Update<'a> = <T as Field>::Update<'a>

The type of the update builder for this field. For embedded types, this is {Type}Update<’a>. For primitives, this will be {Type}Update<’a> once implemented.
Source§

type Inner = <T as Field>::Inner

The unwrapped type used for foreign key filter expressions. Primitives map to themselves. Wrappers like Option<T>, Box<T>, Arc<T>, and Rc<T> map to T::Inner.
Source§

fn new_path<Origin>( path: Path<Origin, <T as Field>::ExprTarget>, ) -> <Deferred<T> as Field>::Path<Origin>

Build a field path from a raw path of the field’s Self::ExprTarget. Read more
Source§

fn new_list_path<Origin>( path: Path<Origin, List<<Deferred<T> as Field>::ExprTarget>>, ) -> <Deferred<T> as Field>::ListPath<Origin>

Build a list field path from a raw path. For primitives, returns the path as-is. For embedded types, wraps the path in a ListFields struct.
Source§

fn new_update<'a>( assignments: &'a mut Assignments, projection: Projection, ) -> <Deferred<T> as Field>::Update<'a>

Build an update builder from assignments and a projection. For primitives, this returns () (no builder). For embedded types, this is overridden to construct the {Type}Update builder.
Source§

fn field_ty(storage_ty: Option<Type>) -> FieldTy

Returns the app-level field type for this primitive. Default implementation returns a Primitive field type. Embedded types override this to return Embedded field type.
Source§

fn key_constraint<Origin>( &self, _target: Path<Origin, <Deferred<T> as Field>::Inner>, ) -> Expr<bool>

Build a boolean filter expression comparing this field value against a target path. Used by generated belongs-to accessors to build FK lookups. Read more
Source§

fn register(model_set: &mut ModelSet)

Register any models referenced by this field type into the given ModelSet. Read more
Source§

impl<T> From<T> for Deferred<T>

Source§

fn from(value: T) -> Deferred<T>

Constructs a loaded Deferred<T> from a value.

Used in struct literals for #[derive(Embed)] types that contain deferred sub-fields, where the user supplies the inner value directly: Metadata { author, notes: "...".into() }.

Source§

impl<T> IntoExpr<T> for Deferred<T>
where T: IntoExpr<T>,

Forwards the inner value’s expression encoding so a Deferred<T> field can be spliced into any site that accepts a T.

Deferred<T> is a load-state wrapper, not a value type, so the produced expression is Expr<T> (the value), never Expr<Deferred<T>>. Panics with the standard “deferred field not loaded” error if the value is in the unloaded state — matching .get() / .into_inner().

Source§

fn into_expr(self) -> Expr<T>

Consume self and produce an Expr<T>. Read more
Source§

fn by_ref(&self) -> Expr<T>

Produce an Expr<T> from a reference without consuming self. Read more
Source§

impl<T> IntoExpr<T> for &Deferred<T>
where T: IntoExpr<T>,

Source§

fn into_expr(self) -> Expr<T>

Consume self and produce an Expr<T>. Read more
Source§

fn by_ref(&self) -> Expr<T>

Produce an Expr<T> from a reference without consuming self. Read more
Source§

impl<T> Load for Deferred<T>
where T: Load<Output = T>,

Source§

type Output = Deferred<T>

The concrete type produced by load. Read more
Source§

fn ty() -> Type

Returns the stmt::Type that describes values of this type.
Source§

fn load(value: Value) -> Result<Deferred<T>, Error>

Deserialize a database Value into Self::Output. Read more
Source§

fn reload(target: &mut Deferred<T>, value: Value) -> Result<(), Error>

Reload the value in-place from a value returned by the database. Read more
Source§

fn ty_relation() -> Type

Returns the stmt::Type used when this type appears as a relation target. The default delegates to ty().
Source§

fn load_relation(value: Value) -> Result<Self::Output, Error>

Deserialize a value that was loaded as a relation target. Read more
Source§

impl<M> RelationManyField for Deferred<Vec<M>>
where M: Model,

Source§

const DEFERRED: bool = true

Whether the field stores its value in a deferred load slot.
Source§

type Target = M

The target model that this field references.
Source§

fn reload(target: &mut Deferred<Vec<M>>, _value: Value) -> Result<(), Error>

Reloads this relation field from a returned value.
Source§

fn many_relation_field_ty( singular: Name, pair: Option<FieldId>, via: Option<Path>, ) -> FieldTy

Build the FieldTy for a HasMany relation field, given the singular name derived from the field identifier and an optional paired BelongsTo field on the target model resolved from #[has_many(pair = <field>)]. When None, the linker selects the pair by searching the target for a unique BelongsTo back to the source. Read more
Source§

impl<M> RelationOneField for Deferred<M>
where M: Model,

Source§

const DEFERRED: bool = true

Whether the field stores its value in a deferred load slot.
Source§

const NULLABLE: bool = false

Whether the field is nullable (i.e. wrapped in Option).
Source§

type Target = M

The target model that this field references.
Source§

type One = <M as Model>::Query<M>

The query type produced by the relation accessor. For non-nullable impls this is <Target as Model>::Query<Target>; for nullable impls it is <Target as Model>::Query<Option<Target>>.
Source§

type Expr = M

The expression-level type used in create/update setters. Resolves to the unwrapped Self::Target for non-nullable impls and Option<Self::Target> for nullable impls.
Source§

fn reload(target: &mut Deferred<M>, _value: Value) -> Result<(), Error>

Reloads this relation field from a returned value.
Source§

fn make_one( query: <<Deferred<M> as RelationOneField>::Target as Model>::Query<List<<Deferred<M> as RelationOneField>::Target>>, ) -> <Deferred<M> as RelationOneField>::One

Narrow a list query targeting the related model into the appropriate “one” query — Query<Model> for non-nullable impls and Query<Option<Model>> for nullable impls. Read more
Source§

fn has_one_relation_field_ty( pair: Option<FieldId>, via: Option<Path>, ) -> FieldTy

Build the FieldTy for a HasOne relation field, given an optional paired BelongsTo field on the target model resolved from #[has_one(pair = <field>)]. When None, the linker selects the pair by searching the target for a unique BelongsTo back to the source. Read more
Source§

fn belongs_to_relation_field_ty(foreign_key: ForeignKey) -> FieldTy

Build the FieldTy for a BelongsTo relation field, given the foreign key resolved from the field’s #[belongs_to(...)] attribute.
Source§

impl<M> RelationOneField for Deferred<Option<M>>
where M: Model,

Source§

const DEFERRED: bool = true

Whether the field stores its value in a deferred load slot.
Source§

const NULLABLE: bool = true

Whether the field is nullable (i.e. wrapped in Option).
Source§

type Target = M

The target model that this field references.
Source§

type One = <M as Model>::Query<Option<M>>

The query type produced by the relation accessor. For non-nullable impls this is <Target as Model>::Query<Target>; for nullable impls it is <Target as Model>::Query<Option<Target>>.
Source§

type Expr = Option<M>

The expression-level type used in create/update setters. Resolves to the unwrapped Self::Target for non-nullable impls and Option<Self::Target> for nullable impls.
Source§

fn reload(target: &mut Deferred<Option<M>>, _value: Value) -> Result<(), Error>

Reloads this relation field from a returned value.
Source§

fn make_one( query: <<Deferred<Option<M>> as RelationOneField>::Target as Model>::Query<List<<Deferred<Option<M>> as RelationOneField>::Target>>, ) -> <Deferred<Option<M>> as RelationOneField>::One

Narrow a list query targeting the related model into the appropriate “one” query — Query<Model> for non-nullable impls and Query<Option<Model>> for nullable impls. Read more
Source§

fn has_one_relation_field_ty( pair: Option<FieldId>, via: Option<Path>, ) -> FieldTy

Build the FieldTy for a HasOne relation field, given an optional paired BelongsTo field on the target model resolved from #[has_one(pair = <field>)]. When None, the linker selects the pair by searching the target for a unique BelongsTo back to the source. Read more
Source§

fn belongs_to_relation_field_ty(foreign_key: ForeignKey) -> FieldTy

Build the FieldTy for a BelongsTo relation field, given the foreign key resolved from the field’s #[belongs_to(...)] attribute.
Source§

impl<T> Serialize for Deferred<T>
where T: Serialize,

Available on crate feature serde only.

Serializes transparently: a loaded Deferred<T> encodes exactly as the inner T, an unloaded one as null.

The unloaded null is not a distinct marker — a loaded Deferred<Option<T>> holding None also encodes as null — so unloaded fields are meant to be skipped rather than serialized. See the type-level docs.

Source§

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

Serialize this value into the given Serde serializer. Read more
Source§

impl<E> ViaManyField for Deferred<Vec<E>>
where E: ViaTarget,

Source§

const DEFERRED: bool = true

Whether the field stores its value in a deferred load slot.
Source§

type Target = E

The via’s terminal type — the model or scalar the path projects.

Auto Trait Implementations§

§

impl<T> Freeze for Deferred<T>
where Option<Box<T>>: Freeze,

§

impl<T> RefUnwindSafe for Deferred<T>

§

impl<T> Send for Deferred<T>
where Option<Box<T>>: Send,

§

impl<T> Sync for Deferred<T>
where Option<Box<T>>: Sync,

§

impl<T> Unpin for Deferred<T>
where Option<Box<T>>: Unpin,

§

impl<T> UnsafeUnpin for Deferred<T>
where Option<Box<T>>: UnsafeUnpin,

§

impl<T> UnwindSafe for Deferred<T>
where Option<Box<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> Assign<Json<T>> for T
where T: Serialize,

Source§

fn into_assignment(self) -> Assignment<Json<T>>

Convert into an Assignment<T> value.
Source§

fn assign(self, assignments: &mut Assignments, projection: Projection)
where Self: Sized,

Record one or more assignments into the given [Assignments] map at the specified projection.
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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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: 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> IntoExpr<Json<T>> for T
where T: Serialize,

Source§

fn into_expr(self) -> Expr<Json<T>>

Consume self and produce an Expr<T>. Read more
Source§

fn by_ref(&self) -> Expr<Json<T>>

Produce an Expr<T> from a reference without consuming self. Read more
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 = !

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