Skip to main content

Module observe

Module observe 

Source
Expand description

Types and traits for observing mutations to data structures.

§How Observers Work

Observers are types that implement DerefMut to the target type being observed. When any method requiring &mut self is called, it triggers the DerefMut hook where change tracking occurs. Additionally, for specific methods like String::push_str and Vec::push, observers provide specialized implementations for more precise tracking.

For types that already implement Deref (like Box<T>), implementing observers is more challenging. If type A dereferences to B, and we have corresponding observers A' and B', where should A' deref to?

  • If A'AB: Changes on B cannot be precisely tracked (because no B' in the dereference chain)
  • If A'B'B: Properties and methods on A become inaccessible (because no A in the dereference chain)

To solve this, we use a Pointer type to create the dereference chain: A'B'Pointer<A>AB. This allows tracking changes on both A and B.

Structs§

DefaultSpec
Default observation specification.
SnapshotSpec
Snapshot-based observation specification.

Traits§

Observe
A trait for types that can be observed for mutations.
ObserveExt
Extension trait providing ergonomic methods for types implementing Observe.
Observer
A trait for observer types that wrap and track mutations to values.
RefObserve
A trait for types whose references can be observed for mutations.
SerializeObserver
Trait for observers that can serialize their recorded mutations.
SerializeObserverExt
Extension trait providing ergonomic methods for SerializeObserver.