Expand description
Typed, shared configuration for services.
SharedCfg stores configuration values by their concrete type. Start
with SharedCfg::new, insert values through SharedCfgBuilder::add,
and finish with SharedCfgBuilder::build. Adding the same type again
replaces its previous value.
Each value implements Configuration and stores a CfgContext.
The builder associates that context with the collection, providing access
to its tag, service name, and other configuration types.
SharedCfg::get returns a Cfg<T> handle that dereferences to T.
If a type was not inserted, lookup creates its default value and caches it
for that collection on the calling thread.
The built collection can be shared across threads, while typed Cfg
handles are local to the thread that obtains them. Configuration is
separate from the state passed to crate::Service operations; a state
value can carry whichever configuration handles the service needs.
A CfgContext keeps the id, tag, and service name of its collection
readable, but does not keep the collection alive. Lookups through a
context panic while the collection is being built and after it has been
dropped; SharedCfg and Cfg handles keep it alive.
§Example
use ntex_service::cfg::{CfgContext, Configuration, SharedCfg};
#[derive(Debug, Default)]
struct Limits {
ctx: CfgContext,
max_requests: usize,
}
impl Configuration for Limits {
const NAME: &'static str = "limits";
fn ctx(&self) -> &CfgContext {
&self.ctx
}
fn set_ctx(&mut self, ctx: CfgContext) {
self.ctx = ctx;
}
}
let shared = SharedCfg::new("listener")
.service("example")
.add(Limits {
max_requests: 32,
..Limits::default()
})
.build();
let limits = shared.get::<Limits>();
assert_eq!(limits.max_requests, 32);
assert_eq!(limits.tag(), "listener");
assert_eq!(limits.service(), "example");§Framework guide
See Service State for passing shared data to services.
Structs§
- Cfg
- A typed handle to a value in a
SharedCfg. - CfgContext
- Metadata and lookup access for a shared configuration.
- Shared
Cfg - An immutable collection of typed configuration values.
- Shared
CfgBuilder - Builder for
SharedCfg.
Traits§
- Configuration
- A value that can be stored in
SharedCfg.