Skip to main content

Tracked

Struct Tracked 

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

Snapshot-based wrapper for explicitly tracked entities.

Tracked<T> keeps the original snapshot together with the current value so later stages can compare and persist changes without relying on runtime proxies or reflection. Registry-owned snapshots keep pending Added, Modified and Deleted work alive after a wrapper is dropped or consumed. Cloning a wrapper copies its visible state and snapshots, but the clone is detached from the registry. Calling Tracked::detach repeatedly is a no-op after the first detach and does not reset the visible wrapper state.

State can be inspected with Tracked::state and changed explicitly with Tracked::mark_modified, Tracked::mark_deleted, Tracked::mark_unchanged and Tracked::detach. Immediate persistence through Tracked::save and Tracked::delete delegates to the same DbSet/Active Record pipelines used by ordinary CRUD.

Implementations§

Source§

impl<T: Clone> Tracked<T>

Source

pub fn from_loaded(entity: T) -> Self

Creates a tracked value loaded from persistence.

Source

pub fn from_added(entity: T) -> Self

Creates a tracked value that represents a new entity pending insertion.

Source§

impl<T> Tracked<T>

Source

pub fn original(&self) -> &T

Returns the original snapshot captured when tracking started.

Source

pub fn current(&self) -> &T

Returns the current in-memory value.

Source

pub const fn state(&self) -> EntityState

Returns the current tracking state.

Source

pub fn mark_modified(&mut self)

Explicitly marks this tracked value as Modified.

Added values remain Added because they still need an insert, and Deleted values remain Deleted because deletion wins over pending modifications until the caller explicitly marks the value unchanged.

Source

pub fn mark_deleted(&mut self)

Explicitly marks this tracked value as Deleted.

This is a state transition only; it does not execute SQL. Calling it for an Added wrapper cancels the pending local insert by detaching the wrapper from the tracker.

Source

pub fn mark_unchanged(&mut self)
where T: Clone + Send + Sync + 'static,

Explicitly accepts the current in-memory value as unchanged.

The current value becomes the new original snapshot and later save_changes() calls ignore this wrapper until it is marked or mutably accessed again.

Source

pub fn detach(&mut self)

Detaches this wrapper from its context tracker without executing SQL.

Detach removes the registration from the current context unit of work and leaves the visible wrapper state unchanged.

Source

pub fn current_mut(&mut self) -> &mut T

Returns mutable access to the current value and marks the entity as modified when it was previously loaded as unchanged.

Source

pub fn save<C>( &mut self, db: &C, ) -> impl Future<Output = Result<(), OrmError>> + Send
where C: DbContextEntitySet<T> + Sync, T: ActiveRecord + AuditEntity + EntityPersist + EntityPrimaryKey + SoftDeleteEntity + TenantScopedEntity + Clone + FromRow + Send,

Persists this tracked entity immediately through the Active Record pipeline and synchronizes the tracking snapshot after success.

This method exists so tracked.save(&db).await has explicit tracking semantics instead of dereferencing to T::save(&db) and leaving the tracker with a stale original snapshot. Unchanged wrappers are a no-op, Added and Modified wrappers use the same persistence path as Active Record, and Deleted wrappers return an error.

Source

pub fn delete<C>( &mut self, db: &C, ) -> impl Future<Output = Result<bool, OrmError>> + Send
where C: DbContextEntitySet<T> + Sync, T: ActiveRecord + EntityPersist + EntityPrimaryKey + SoftDeleteEntity + TenantScopedEntity + Clone + FromRow + Send,

Deletes this tracked entity immediately through the Active Record pipeline and removes it from the context tracker after success.

Calling tracked.delete(&db).await on an Added wrapper cancels the local insert without touching the database. Persisted wrappers delegate to Active Record delete and detach after the row is affected, so a later save_changes() will not issue a second delete for the same wrapper.

Source§

impl<T: Clone> Tracked<T>

Source

pub fn into_current(self) -> T

Consumes the tracked wrapper and returns the current entity value.

Trait Implementations§

Source§

impl<T: Clone> Clone for Tracked<T>

Source§

fn clone(&self) -> Self

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> Debug for Tracked<T>

Source§

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

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

impl<T> Deref for Tracked<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T> DerefMut for Tracked<T>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<T> Drop for Tracked<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<T: PartialEq> PartialEq for Tracked<T>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Eq> Eq for Tracked<T>

Auto Trait Implementations§

§

impl<T> Freeze for Tracked<T>

§

impl<T> RefUnwindSafe for Tracked<T>
where T: RefUnwindSafe,

§

impl<T> Send for Tracked<T>
where T: Send,

§

impl<T> Sync for Tracked<T>
where T: Sync,

§

impl<T> Unpin for Tracked<T>

§

impl<T> UnsafeUnpin for Tracked<T>

§

impl<T> UnwindSafe for Tracked<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: 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<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> 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