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
use CancellationToken;
use crateLifecycleBus;
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 runtime_rs::{LifecycleBus, Registry, SharedState};
///
/// #[derive(Clone)]
/// pub struct AppState(Arc<Inner>);
///
/// struct Inner {
/// shutdown_token: CancellationToken,
/// registry: Registry<AppState>,
/// events: LifecycleBus,
/// }
///
/// impl SharedState for AppState {
/// fn shutdown_token(&self) -> CancellationToken {
/// self.0.shutdown_token.clone()
/// }
///
/// fn registry_ref(&self) -> &Registry<Self> {
/// &self.0.registry
/// }
///
/// fn events(&self) -> &LifecycleBus {
/// &self.0.events
/// }
/// }
/// ```