pub struct DirtyTracker { /* private fields */ }Expand description
脏字段追踪器
通过维护「原始值快照」与「当前值」两份 HashMap,比较得到脏字段集合。
§设计要点
- 快照机制:
new()时将所有字段视为「原始值」 - 修改追踪:
set()修改字段时,仅写入current,不动original - 脏字段判定:
original[field] != current[field]则该字段为脏 - 新字段处理:
set()一个 original 中不存在的字段时,自动视为脏字段 (等价于original[field] = Null) - mark_clean:写入成功后调用,将
original同步为current - rollback:写入失败时调用
rollback(),将current还原为original
Implementations§
Source§impl DirtyTracker
impl DirtyTracker
Sourcepub fn empty() -> DirtyTracker
pub fn empty() -> DirtyTracker
创建空追踪器
Sourcepub fn set(&mut self, field: impl Into<String>, value: Value)
pub fn set(&mut self, field: impl Into<String>, value: Value)
设置字段值(修改 current,不动 original)
若字段原本不存在(original 中无),则视为新增脏字段。
Sourcepub fn get_original(&self, field: &str) -> Option<&Value>
pub fn get_original(&self, field: &str) -> Option<&Value>
获取原始值(写入失败时可用于回滚业务层)
Sourcepub fn is_field_dirty(&self, field: &str) -> bool
pub fn is_field_dirty(&self, field: &str) -> bool
判断指定字段是否为脏
Sourcepub fn get_dirty_fields(&self) -> Vec<String>
pub fn get_dirty_fields(&self) -> Vec<String>
获取所有脏字段名(按字段名字典序排列,保证测试稳定)
Sourcepub fn get_dirty_attributes(&self) -> HashMap<String, Value>
pub fn get_dirty_attributes(&self) -> HashMap<String, Value>
获取脏字段及其当前值
Sourcepub fn mark_clean(&mut self)
pub fn mark_clean(&mut self)
标记所有字段为干净(写入成功后调用)
将 original 同步为 current,下一次 is_dirty() 返回 false。
Trait Implementations§
Source§impl Clone for DirtyTracker
impl Clone for DirtyTracker
Source§fn clone(&self) -> DirtyTracker
fn clone(&self) -> DirtyTracker
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for DirtyTracker
impl RefUnwindSafe for DirtyTracker
impl Send for DirtyTracker
impl Sync for DirtyTracker
impl Unpin for DirtyTracker
impl UnsafeUnpin for DirtyTracker
impl UnwindSafe for DirtyTracker
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more