topcoat-core 0.12.0

A modular, batteries-included Rust web framework for server-rendered apps.
Documentation
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//! Records context reads so `#[memoize]` can decide when to reuse a result.

use std::{
    any::{Any, TypeId},
    sync::{Arc, Mutex},
};

use crate::context::{BindingId, Scope};

/// A saved context read, including its type and what it found.
///
/// A saved read matches a context if reading its type there finds the same
/// thing. Shadowing the value breaks that match. If the read found nothing,
/// adding a value of that type breaks the match too.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct ContextRead {
    type_id: TypeId,
    found: Found,
}

/// What a saved read found.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Found {
    /// The closest value of the type, or `None` if there was none.
    Closest(Option<BindingId>),
    /// Every value of the type.
    ///
    /// `request` is the newest request value, whose ID identifies all
    /// request values of the type. `sealed` is the newest sealed layer,
    /// whose ID identifies all sealed layers.
    All {
        request: Option<BindingId>,
        sealed: Option<BindingId>,
    },
}

impl ContextRead {
    /// Records that `T` resolved to `binding_id`, or to nothing at all.
    pub(crate) fn new<T>(binding_id: Option<BindingId>) -> Self
    where
        T: Any,
    {
        Self {
            type_id: TypeId::of::<T>(),
            found: Found::Closest(binding_id),
        }
    }

    /// Records that every value of `T` in `scope` was read.
    pub(crate) fn all<T>(scope: &Scope) -> Self
    where
        T: Any,
    {
        Self::all_of(TypeId::of::<T>(), scope)
    }

    /// Records that every value of `type_id` in `scope` was read.
    fn all_of(type_id: TypeId, scope: &Scope) -> Self {
        Self {
            type_id,
            found: Found::All {
                request: scope.request_binding_id(type_id),
                sealed: scope.sealed_id(),
            },
        }
    }

    /// Checks whether reading this read's type in `scope` would find the
    /// same values.
    pub(crate) fn matches(&self, scope: &Scope) -> bool {
        match self.found {
            Found::Closest(binding_id) => scope.binding_id(self.type_id) == binding_id,
            Found::All { .. } => Self::all_of(self.type_id, scope) == *self,
        }
    }

    /// Re-expresses this read relative to `entry`, the scope a tracked call
    /// started with. Returns `None` if the read does not depend on `entry`.
    ///
    /// A closest read that `entry` does not match found a value the call
    /// added itself, so it is dropped. A read of every value is kept but
    /// re-read from `entry`. Values the call added only stack on top of the
    /// values of `entry`, so the walk also visited every value of `entry`.
    fn relative_to(&self, entry: &Scope) -> Option<Self> {
        match self.found {
            Found::Closest(_) => self.matches(entry).then_some(*self),
            Found::All { .. } => Some(Self::all_of(self.type_id, entry)),
        }
    }
}

/// Collects context reads for one function call.
///
/// [`Cx::track`] attaches the tracker to a handle. Children and clones keep
/// it, including clones sent to other threads. The tracker compares reads
/// with the context at the start of the call. It ignores reads of values
/// added by the function itself, since those do not depend on the caller.
///
/// [`Cx`]: crate::context::Cx
/// [`Cx::track`]: crate::context::Cx::track
#[derive(Debug)]
pub(crate) struct ContextTracker {
    /// The scope the tracked call was entered with.
    entry: Arc<Scope>,
    reads: Mutex<Vec<ContextRead>>,
}

impl ContextTracker {
    /// Creates a tracker for a call entered with the scope `entry`.
    pub(crate) fn new(entry: Arc<Scope>) -> Self {
        Self {
            entry,
            reads: Mutex::new(Vec::new()),
        }
    }

    /// Records `read`, unless it only found a binding the tracked call
    /// created itself.
    pub(crate) fn record(&self, read: ContextRead) {
        self.merge(&[read]);
    }

    /// Records every read in `reads` relative to the entry scope, skipping
    /// reads already recorded.
    ///
    /// Replaying a nested tracked call's reads through this re-expresses them
    /// relative to this tracker's own entry scope: a closest read whose value
    /// was shadowed after tracking started resolves through a binding the
    /// tracked call created itself, so it is not a dependency and is dropped,
    /// even where it was a genuine dependency of the nested call. A read of
    /// every value is re-read from the entry scope instead, since the walk
    /// continues into the values of the entry scope.
    pub(crate) fn merge(&self, reads: &[ContextRead]) {
        // Skip the lock when the function read no context values.
        if reads.is_empty() {
            return;
        }
        let mut own = self.reads.lock().expect("context tracker lock poisoned");
        for read in reads {
            if let Some(read) = read.relative_to(&self.entry)
                && !own.contains(&read)
            {
                own.push(read);
            }
        }
    }

    /// Returns the reads recorded so far, leaving the tracker empty.
    ///
    /// Called once, when the tracked call is done and its reads are handed to
    /// the variant they belong to, so the recorded reads are moved out rather
    /// than copied.
    pub(crate) fn take_reads(&self) -> Vec<ContextRead> {
        std::mem::take(&mut *self.reads.lock().expect("context tracker lock poisoned"))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::context::{Cx, context, iter_context, try_context};

    struct Session;
    struct Theme;

    /// Observes what `cx`'s scope currently resolves `T` to, for building
    /// expected reads.
    fn expected<T: Any>(cx: &Cx) -> ContextRead {
        ContextRead::new::<T>(cx.scope().binding_id(TypeId::of::<T>()))
    }

    #[test]
    fn a_present_read_matches_its_own_scope() {
        let cx = Cx::new().with(Session);

        let read = expected::<Session>(&cx);
        assert!(read.matches(cx.scope()));
    }

    #[test]
    fn shadowing_invalidates_a_present_read() {
        let cx = Cx::new().with(Session);
        let read = expected::<Session>(&cx);

        let shadowed = cx.with(Session);

        assert!(read.matches(cx.scope()));
        assert!(!read.matches(shadowed.scope()));
    }

    #[test]
    fn an_absence_read_matches_while_the_type_is_absent() {
        let cx = Cx::new();
        let read = expected::<Session>(&cx);
        assert!(read.matches(cx.scope()));
    }

    #[test]
    fn registering_the_type_invalidates_an_absence_read() {
        let cx = Cx::new();
        let read = expected::<Session>(&cx);

        let extended = cx.with(Session);

        assert!(read.matches(cx.scope()));
        assert!(!read.matches(extended.scope()));
    }

    #[test]
    fn an_unrelated_binding_leaves_a_read_valid() {
        let cx = Cx::new().with(Session);
        let read = expected::<Session>(&cx);

        let extended = cx.with(Theme);

        assert!(read.matches(extended.scope()));
    }

    #[test]
    fn a_present_read_is_recorded() {
        let cx = Cx::new().with(Session);
        let (child, tracker) = cx.track();

        let _: &Session = context(&child);

        assert_eq!(tracker.take_reads(), [expected::<Session>(&cx)]);
    }

    #[test]
    fn an_absence_read_is_recorded() {
        let cx = Cx::new();
        let (child, tracker) = cx.track();

        assert!(try_context::<Session>(&child).is_none());
        assert_eq!(tracker.take_reads(), [expected::<Session>(&cx)]);
    }

    #[test]
    fn a_repeated_read_is_recorded_once() {
        let cx = Cx::new().with(Session);
        let (child, tracker) = cx.track();

        let _ = try_context::<Session>(&child);
        let _ = try_context::<Session>(&child);

        assert_eq!(tracker.take_reads(), [expected::<Session>(&cx)]);
    }

    #[test]
    fn distinct_reads_are_all_recorded() {
        let cx = Cx::new().with(Session);
        let (child, tracker) = cx.track();

        let _ = try_context::<Session>(&child);
        let _ = try_context::<Theme>(&child);

        assert_eq!(
            tracker.take_reads(),
            [expected::<Session>(&cx), expected::<Theme>(&cx)]
        );
    }

    #[test]
    fn an_inherited_read_through_a_derived_scope_is_recorded() {
        let cx = Cx::new().with(Session);
        let (child, tracker) = cx.track();
        let derived = child.with(Theme);

        let _ = try_context::<Session>(&derived);

        assert_eq!(tracker.take_reads(), [expected::<Session>(&cx)]);
    }

    #[test]
    fn a_read_of_an_internal_binding_is_not_recorded() {
        let cx = Cx::new();
        let (child, tracker) = cx.track();
        let derived = child.with(Theme);

        let _ = try_context::<Theme>(&derived);

        assert_eq!(tracker.take_reads(), []);
    }

    #[test]
    fn a_read_of_an_internal_shadowing_binding_is_not_recorded() {
        let cx = Cx::new().with(Session);
        let (child, tracker) = cx.track();
        let derived = child.with(Session);

        let _ = try_context::<Session>(&derived);

        assert_eq!(tracker.take_reads(), []);
    }

    #[test]
    fn a_read_through_the_untracked_original_is_not_recorded() {
        let cx = Cx::new().with(Session);
        let (_child, tracker) = cx.track();

        let _ = try_context::<Session>(&cx);

        assert_eq!(tracker.take_reads(), []);
    }

    #[test]
    fn a_read_through_a_clone_of_the_tracked_handle_is_recorded() {
        let cx = Cx::new().with(Session);
        let (child, tracker) = cx.track();

        let _ = try_context::<Session>(&child.clone());

        assert_eq!(tracker.take_reads(), [expected::<Session>(&cx)]);
    }

    #[test]
    fn a_read_on_another_thread_is_recorded() {
        let cx = Cx::new().with(Session);
        let (child, tracker) = cx.track();

        std::thread::spawn(move || {
            let _ = try_context::<Session>(&child);
        })
        .join()
        .expect("tracked thread panicked");

        assert_eq!(tracker.take_reads(), [expected::<Session>(&cx)]);
    }

    #[test]
    fn merge_applies_the_entry_filter_and_deduplicates() {
        let cx = Cx::new().with(Session);
        let (child, tracker) = cx.track();
        let scoped = child.with(Theme);
        let inherited = expected::<Session>(&cx);
        let internal = expected::<Theme>(&scoped);

        tracker.merge(&[inherited, internal, inherited]);

        assert_eq!(tracker.take_reads(), [inherited]);
    }

    #[test]
    fn shadowing_invalidates_an_iteration_read() {
        let cx = Cx::new().with(Session);
        let read = ContextRead::all::<Session>(cx.scope());

        let shadowed = cx.with(Session);

        assert!(read.matches(cx.scope()));
        assert!(!read.matches(shadowed.scope()));
    }

    #[test]
    fn an_unrelated_binding_leaves_an_iteration_read_valid() {
        let cx = Cx::new().with(Session);
        let read = ContextRead::all::<Session>(cx.scope());

        let extended = cx.with(Theme);

        assert!(read.matches(extended.scope()));
    }

    #[test]
    fn other_sealed_layers_invalidate_an_iteration_read() {
        let cx = Cx::new().with_shared(Arc::default());
        let read = ContextRead::all::<Session>(cx.scope());

        assert!(!read.matches(Cx::new().with_shared(Arc::default()).scope()));
    }

    #[test]
    fn an_iteration_read_is_recorded() {
        let cx = Cx::new().with(Session);
        let (child, tracker) = cx.track();

        let _ = iter_context::<Session>(&child).count();

        assert_eq!(
            tracker.take_reads(),
            [ContextRead::all::<Session>(cx.scope())]
        );
    }

    #[test]
    fn an_iteration_through_an_internal_binding_is_recorded_from_the_entry() {
        let cx = Cx::new().with(Session);
        let (child, tracker) = cx.track();
        let derived = child.with(Session);

        let _ = iter_context::<Session>(&derived).count();

        assert_eq!(
            tracker.take_reads(),
            [ContextRead::all::<Session>(cx.scope())]
        );
    }

    #[test]
    fn closest_and_iteration_reads_of_one_type_are_both_recorded() {
        let cx = Cx::new().with(Session);
        let (child, tracker) = cx.track();

        let _ = try_context::<Session>(&child);
        let _ = iter_context::<Session>(&child).count();

        assert_eq!(
            tracker.take_reads(),
            [
                expected::<Session>(&cx),
                ContextRead::all::<Session>(cx.scope())
            ]
        );
    }

    #[test]
    fn a_nested_track_replaces_the_inherited_tracker() {
        let cx = Cx::new().with(Session);
        let (child, outer) = cx.track();
        let (nested, inner) = child.track();

        let _ = try_context::<Session>(&nested);

        assert_eq!(outer.take_reads(), []);
        assert_eq!(inner.take_reads(), [expected::<Session>(&cx)]);
    }
}