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
use CancellationToken;
use crateProcessEventBus;
use crateRegistry;
/// Shared application state passed to providers and runtime tasks.
///
/// Applications keep ownership of their real state. Put config handles,
/// typed services, caches, event buses, and domain state wherever they belong;
/// this trait only says how the runtime observes process shutdown.
///
/// The recommended shape is a cheap cloneable handle around a private `Inner`.
/// This keeps state clones cheap while letting `registry_ref()` and `events()`
/// return ordinary references:
///
/// ```text
/// use std::sync::Arc;
/// use tokio_util::sync::CancellationToken;
/// use continuo::{ProcessEventBus, Registry, SharedState};
///
/// #[derive(Clone)]
/// pub struct AppState(Arc<Inner>);
///
/// struct Inner {
/// shutdown_token: CancellationToken,
/// registry: Registry<AppState>,
/// events: ProcessEventBus,
/// }
///
/// impl SharedState for AppState {
/// fn shutdown_token(&self) -> CancellationToken {
/// self.0.shutdown_token.clone()
/// }
/// }
///
/// impl HasRegistry for AppState {
/// fn registry_ref(&self) -> &Registry<Self> {
/// &self.0.registry
/// }
/// }
///
/// impl HasEvents for AppState {
/// fn events(&self) -> &ProcessEventBus {
/// &self.0.events
/// }
/// }
/// ```
/// A state that owns a [`Registry`].
///
/// A trait of its own rather than a `#[cfg]`-gated method on [`SharedState`].
/// Cargo features are additive and unify across the whole graph, so a gated
/// *method* means any crate anywhere enabling `registry` adds a required method
/// to a trait other crates have already implemented, and their builds break at
/// a distance for a feature they never asked for. A gated *trait* adds a
/// capability instead: enabling the feature offers something new and takes
/// nothing away.
/// A state that owns a [`ProcessEventBus`]. Separate from [`SharedState`] for
/// the same reason as [`HasRegistry`].