runtime_injector
runtime_injector copied to clipboard
Allow injectors to have their provider map modified after being built
Suppose a config is reloaded during execution of a program. This would allow services to be reconfigured based on the changes in that config. Currently, it's somewhat possible using factories:
struct FooFactory {
injector: Injector,
config: Svc<Mutex<Config>>
}
impl FooFactory {
pub fn new(injector: Injector, config: Svc<Mutex<Config>>) -> Self {
FooFactory {
injector,
config,
}
}
pub fn get(&self) -> InjectResult<Svc<dyn Foo>> {
match self.config.lock().unwrap().foo_impl {
FooImpl::Bar => self.injector.get::<Svc<Bar>>().map(|bar| bar as Svc<dyn Foo>),
// ...
}
}
}
This is a lot of work to reconfigure just a single service based on a config reloaded at runtime. It might be better to instead expose some functions to mutate the provider map in existing Injector
s.