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
//! What:
//! Call a function with its state and get the panic it raised as a value,
//! not as an unwind.
//! Why:
//! `std::panic::catch_unwind` accepts only a callable with no arguments,
//! so a guarded call that needs the caller's borrows
//! would have to be a closure at every place that guards one.
//! This is the one place that makes the callable,
//! so the guarded
//! calls elsewhere are a function and the state it reads.
//!
//! In TS you'd write (pseudocode):
//! ```ts
//! try { return { ok: call(state) }; } catch (panic) { return { panic }; }
//! ```
/// What:
/// Run `call(state)` and return what it returned,
/// or the payload of the panic it raised.
/// Why:
/// The state is asserted to be safe to look at after a panic,
/// as the callers did when they wrapped a closure the same way:
/// each caller discards or reports the panicked work and never reads half-updated state.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// const caught = callCatchingPanics(state, call); // { ok } or { panic }
/// ```
/// Panic catching controls stay out of the release executable.