Skip to main content

WithRelated

Struct WithRelated 

Source
pub struct WithRelated<M> {
    pub inner: M,
    /* private fields */
}
Expand description

A model instance with an attached relation cache (level-1, in-process).

Derefs to the inner model so field access (post.title) works directly. Relations are populated by QueryBuilder::all() when .select_related() or .prefetch_related() were chained before execution.

§Example

ⓘ
let posts = Post::filter(&pool)
    .select_related(Post::AUTHOR_ID)
    .prefetch_related(Comment::POST_ID)
    .all()
    .await?;

for post in &posts {
    println!("{} by {}", post.title, post.related::<Author>().name);
    for comment in post.prefetched::<Comment>() {
        println!("  - {}", comment.body);
    }
}

Fields§

§inner: M

Implementations§

Source§

impl<M> WithRelated<M>

Source

pub fn into_inner(self) -> M

The model, dropping the loaded relations.

Source

pub fn related<R: Any + Send + Sync + 'static>(&self) -> &R

Returns the FK-related instance loaded by select_related.

§Panics

Panics with a clear message if .select_related(..) was not called for R on the query builder.

Source

pub fn prefetched<R: Any + Send + Sync + 'static>(&self) -> &[R]

Returns the prefetched slice loaded by prefetch_related.

§Panics

Panics with a clear message if .prefetch_related(..) was not called for R on the query builder.

Trait Implementations§

Source§

impl<M> Deref for WithRelated<M>

Source§

type Target = M

The resulting type after dereferencing.
Source§

fn deref(&self) -> &M

Dereferences the value.
Source§

impl<M> DerefMut for WithRelated<M>

Source§

fn deref_mut(&mut self) -> &mut M

Mutably dereferences the value.

Auto Trait Implementations§

§

impl<M> !RefUnwindSafe for WithRelated<M>

§

impl<M> !UnwindSafe for WithRelated<M>

§

impl<M> Forget for WithRelated<M>

§

impl<M> Freeze for WithRelated<M>
where M: Freeze,

§

impl<M> Send for WithRelated<M>
where M: Send,

§

impl<M> Sync for WithRelated<M>
where M: Sync,

§

impl<M> Unpin for WithRelated<M>
where M: Unpin,

§

impl<M> UnsafeUnpin for WithRelated<M>
where M: UnsafeUnpin,

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> 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.