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
//! The [`ConfigScope`] trait and the [`SharedScope`] handle it works with.
use Future;
use Arc;
use RwLock;
use crateResult;
use crateConfigStore;
/// Shared handle to a scope's configuration and runtime state.
///
/// This is an alias for [`Arc<RwLock<T>>`], not a separate wrapper: Tokio's
/// [`read`](RwLock::read), [`write`](RwLock::write), [`try_read`](RwLock::try_read)
/// and [`try_write`](RwLock::try_write) methods are directly available.
/// Cloning the handle shares the same value and lock; it does not copy `T`.
///
/// Keep guards short-lived. A guard can cross an `.await`, but the lock is not
/// reentrant: drop every guard before calling [`ConfigScope::load`] or
/// [`ConfigScope::save`] on this handle in the same task. A held read guard can
/// also block `save` when a writer is queued, because Tokio's lock is fair.
/// The [guide](https://docs.rs/conferencier/%5E0.1.0/conferencier/guide/concepts/index.html#locking)
/// compares this lock with the store's.
///
/// # Example
///
/// ```
/// use std::sync::Arc;
/// use conferencier::SharedScope;
/// use tokio::sync::RwLock;
///
/// # #[tokio::main]
/// # async fn main() {
/// let scope: SharedScope<u64> = Arc::new(RwLock::new(0));
/// let task_scope = scope.clone();
/// tokio::spawn(async move {
/// *task_scope.write().await += 1;
/// })
/// .await
/// .expect("task panicked");
///
/// assert_eq!(*scope.read().await, 1);
/// # }
/// ```
pub type SharedScope<T> = ;
/// A component's typed configuration and runtime state, shared between Tokio tasks.
///
/// A derived scope owns one top-level TOML section. Loaded fields describe the
/// settings persisted in that section; fields marked `#[confer(ignore)]` or
/// `#[confer(ignore = <expr>)]` hold runtime state that is not persisted.
/// Nested configuration values use [`ConfigTable`](crate::ConfigTable).
///
/// # Store and scope are separate
///
/// [`from_store`](Self::from_store), [`load`](Self::load) and [`save`](Self::save)
/// explicitly copy data between a scope and a [`ConfigStore`]. A derived scope
/// does not retain the store or synchronize with it in the background. Changing
/// the store does not change an existing scope until `load` is called; changing
/// a scope does not change the store until `save` is called. None of these
/// functions reads or writes a file.
///
/// The library does not start a Tokio runtime or choose its flavor. Derived
/// `from_store`, `load` and `save` need no runtime unless caller-supplied defaults,
/// ignore expressions, or manual implementations require one.
///
/// # Implementations
///
/// With the default `derive` feature, `#[derive(ConfigScope)]` implements this
/// trait for named-field structs; the [derive guide](https://docs.rs/conferencier/%5E0.1.0/conferencier/guide/derive/index.html)
/// describes how its attributes select the section and map fields.
///
/// Manual implementations remain supported throughout 0.1.x. Their required
/// signatures, bounds and documented behavior are stable, and neither this
/// trait nor `ConfigTable` gains methods during that series.
///
/// Manual implementations can write `async fn load` and `async fn save`; the compiler
/// checks that the returned futures are `Send`, so generic code can spawn them on Tokio tasks.
/// No `#[async_trait]` is needed. Manual implementations are responsible for the
/// loading, saving and error semantics described by this trait.
///
/// # Example
///
/// ```
/// # #[cfg(feature = "derive")]
/// # fn main() -> conferencier::Result<()> {
/// use conferencier::{ConfigScope, ConfigStore};
///
/// #[derive(ConfigScope)]
/// #[confer(section = "App")]
/// struct AppConfig {
/// port: u16,
/// #[confer(ignore)]
/// requests: u64,
/// }
///
/// let store = ConfigStore::from_str("[App]\nport = 8080")?;
/// let app = AppConfig::from_store(&store)?; // No runtime needed for construction.
///
/// # tokio::runtime::Runtime::new()?.block_on(async {
/// app.write().await.requests = 1;
/// store.set_integer("App", "port", 9090)?;
/// AppConfig::load(&app, &store).await?;
/// {
/// let state = app.read().await;
/// assert_eq!(state.port, 9090);
/// assert_eq!(state.requests, 1);
/// }
///
/// app.write().await.port = 9000;
/// AppConfig::save(&app, &store).await?;
/// assert_eq!(store.get_integer("App", "port")?, 9000);
/// # Ok(())
/// # })
/// # }
/// # #[cfg(not(feature = "derive"))]
/// # fn main() {}
/// ```