1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//! [`QuasiObserver`] trait, [`Invalidate`] trait, and autoref-based specialization for the
//! [`observe!`](crate::observe!) macro.
//!
//! The [`observe!`](crate::observe!) macro transforms assignments (`lhs = rhs`) and comparisons
//! (`lhs == rhs`) into calls to [`tracked_mut`](QuasiObserver::tracked_mut) and
//! [`untracked_ref`](QuasiObserver::untracked_ref). These methods are implemented for both
//! observer types and plain references (`&T`, `&mut T`), so Rust's autoref-based method resolution
//! selects the correct implementation depending on whether the operand is an observer or a plain
//! value.
//!
//! [`Invalidate`] is the companion trait for types that carry internal tracking state (diff
//! trackers, inner observer containers). It provides a single
//! [`invalidate`](Invalidate::invalidate) entry point used by the fallback invalidation
//! mechanism in [`Pointer`].
//!
//! See the [Observer Mechanism](https://github.com/shigma/muon#observer-mechanism) section in
//! the README for a detailed overview.
use ;
use crate;
/// Enables [`tracked_mut`](QuasiObserver::tracked_mut) and
/// [`untracked_mut`](QuasiObserver::untracked_mut) to reach the [`Pointer`] without triggering
/// [`DerefMut`] on any observer layer.
///
/// The default implementation falls through to [`DerefMut`]. The key specialization is for
/// [`Pointer<S>`](Pointer), which uses immutable coinductive traversal followed by unsafe
/// interior-mutable access, bypassing all [`DerefMut`] hooks.
/// Formalizes the dereference chain and provides the three mutation tracking primitives:
/// [`untracked_ref`](Self::untracked_ref), [`untracked_mut`](Self::untracked_mut), and
/// [`tracked_mut`](Self::tracked_mut).
///
/// Also implemented for `&T`, `&mut T`, and [`Pointer<T>`] (where all methods reduce to identity),
/// enabling the [`observe!`](crate::observe!) macro to work uniformly with both observers and plain
/// references via autoref-based specialization. The name "quasi-observer" reflects this dual
/// nature — plain references are not real observers, but they participate in the same interface.
///
/// ## Primitives of Mutation Tracking
///
/// | Method | Receiver | Triggers Invalidation |
/// | -------------------------------------- | ----------- | --------------------- |
/// | [`untracked_ref`](Self::untracked_ref) | `&self` | No |
/// | [`untracked_mut`](Self::untracked_mut) | `&mut self` | No |
/// | [`tracked_mut`](Self::tracked_mut) | `&mut self` | Yes |
///
/// ## Dereference Chain
///
/// A `QuasiObserver` defines a two-segment dereference chain:
///
/// ```text
/// Self --[OuterDepth]-> Pointer<Head> --> Head --[InnerDepth]-> Target
/// (coinductive) (inductive)
/// ```
///
/// - [`OuterDepth`](QuasiObserver::OuterDepth): The number of coinductive dereferences from `Self`
/// to its internal [`Pointer`]. For a simple observer this is `Succ<Zero>` (one). For a composite
/// observer like [`VecObserver`](crate::impls::VecObserver) which wraps
/// [`SliceObserver`](crate::impls::SliceObserver), it is `Succ<Succ<Zero>>` (two). For `&T`,
/// `&mut T`, and [`Pointer<T>`] it is `Zero`.
///
/// - [`InnerDepth`](QuasiObserver::InnerDepth): The number of inductive dereferences from the
/// [`Head`](Self::Head) type (stored inside the [`Pointer`]) to the final observed type. For
/// example, when observing a [`Vec<T>`], the [`Head`](Self::Head) is [`Vec<T>`] and the observed
/// type is `[T]`, so `InnerDepth = Succ<Zero>`.
///
/// ## Implementation Notes
///
/// 1. **Every type implementing [`Observer`](crate::observe::Observer) should manually implement
/// [`QuasiObserver`]**. Without this implementation, assignments and comparisons in the
/// [`observe!`](crate::observe!) macro may not work as expected, potentially causing compilation
/// errors or incorrect behavior. We cannot provide a blanket implementation `impl<T: Observer>
/// QuasiObserver for T` because it would conflict with the `impl<T> QuasiObserver for &T` and
/// `impl<T> QuasiObserver for &mut T` implementations.
///
/// 2. **Do not implement [`QuasiObserver`] for types other than `&T`, `&mut T`, [`Pointer<T>`], and
/// [`Observer`](crate::observe::Observer) types**. Implementing [`QuasiObserver`] for other
/// [`Deref`] types (like [`Box`], [`MutexGuard`](std::sync::MutexGuard), etc.) may cause
/// unexpected behavior in the [`observe!`](crate::observe!) macro, as it would interfere with
/// the autoref-based specialization mechanism.
/// Invalidation hook for state types that track an observed value.
///
/// Registered with [`Pointer::register_state`] for fallback invalidation. The method is named
/// `invalidate` rather than `mark_replace` to avoid coupling with
/// [`MutationKind`](crate::MutationKind).
///
/// The type parameter `T` represents the observed value type. `Invalidate<()>` represents
/// "blind" invalidation without access to the current value.