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>
impl<T: Clone> Tracked<T>
Sourcepub fn from_loaded(entity: T) -> Self
pub fn from_loaded(entity: T) -> Self
Creates a tracked value loaded from persistence.
Sourcepub fn from_added(entity: T) -> Self
pub fn from_added(entity: T) -> Self
Creates a tracked value that represents a new entity pending insertion.
Source§impl<T> Tracked<T>
impl<T> Tracked<T>
Sourcepub const fn state(&self) -> EntityState
pub const fn state(&self) -> EntityState
Returns the current tracking state.
Sourcepub fn mark_modified(&mut self)
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.
Sourcepub fn mark_deleted(&mut self)
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.
Sourcepub fn mark_unchanged(&mut self)
pub fn mark_unchanged(&mut self)
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.
Sourcepub fn detach(&mut self)
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.
Sourcepub fn current_mut(&mut self) -> &mut T
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.
Sourcepub fn save<C>(
&mut self,
db: &C,
) -> impl Future<Output = Result<(), OrmError>> + Sendwhere
C: DbContextEntitySet<T> + Sync,
T: ActiveRecord + AuditEntity + EntityPersist + EntityPrimaryKey + SoftDeleteEntity + TenantScopedEntity + Clone + FromRow + Send,
pub fn save<C>(
&mut self,
db: &C,
) -> impl Future<Output = Result<(), OrmError>> + Sendwhere
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.
Sourcepub fn delete<C>(
&mut self,
db: &C,
) -> impl Future<Output = Result<bool, OrmError>> + Sendwhere
C: DbContextEntitySet<T> + Sync,
T: ActiveRecord + EntityPersist + EntityPrimaryKey + SoftDeleteEntity + TenantScopedEntity + Clone + FromRow + Send,
pub fn delete<C>(
&mut self,
db: &C,
) -> impl Future<Output = Result<bool, OrmError>> + Sendwhere
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.