You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Mar 15, 2025. It is now read-only.
Hey, I started playing around with this DI container but I'm unable to achieve interior mutability. Could you provide some examples how that could be achieved with both Svc modes (Rc/Arc) and:
Cell,
RefCell,
Mutex,
RwLock,
or any other wrapper of any kind?
I've only found the example for constant with the Mutex in one of the docstrings:
builder.provide(constant(Mutex::new(0i32)));
I would like to be able to provide particular component wrapped for example by Mutex and then be able to retrieve this Mutex from the container.
I've created small example with comments what I would like to be able to achieve:
use runtime_injector::*;use std::cell::{Cell,RefCell};use std::error::Error;use std::sync::{Mutex,RwLock};pubtraitFoo:Service{fnincrease(&mutself);}#[derive(Default)]structFooImpl(u64);implFooforFooImpl{fnincrease(&mutself){self.0 += 1;}}pubtraitBar:Service{fnincrease(&mutself);}structBarImpl{foo:Svc<dynFoo>,}implBarImpl{// Not possible to get Cell/RefCell/Mutex/RwLockpubfnnew(foo:Svc<dynFoo>) -> Self{Self{ foo }}}implBarforBarImpl{fnincrease(&mutself){self.foo.increase();}}interface!(
dyn Foo = [FooImpl],
dyn Bar = [BarImpl]);fnmain() -> Result<(),Box<dynError>>{letmut builder = Injector::builder();// No obvious way to allow interior mutability, like auto creation of Cell/RefCell/Mutex/RwLock
builder.provide(FooImpl::default.singleton().with_interface::<dynFoo>());
builder.provide(BarImpl::new.singleton().with_interface::<dynBar>());// It could be available for example in such way:
builder.provide(FooImpl::default.wrap_in_mutex().singleton().with_interface::<dynFoo>());// or:
builder.provide(FooImpl::default.wrap_using(|result| Mutex::new(result)).singleton().with_interface::<dynFoo>());let injector = builder.build();// Not able to call these as they require &mut
injector.get::<Svc<dynFoo>>()?.increase();
injector.get::<Svc<dynBar>>()?.increase();// No Cell/RefCell/Mutex/RwLock availability
injector.get::<Cell<Svc<dynBar>>>()?.increase();
injector.get::<RefCell<Svc<dynBar>>>()?.increase();
injector.get::<Mutex<Svc<dynBar>>>()?.increase();
injector.get::<RwLock<Svc<dynBar>>>()?.increase();Ok(())}
Hey, I started playing around with this DI container but I'm unable to achieve interior mutability. Could you provide some examples how that could be achieved with both
Svcmodes (Rc/Arc) and:Cell,RefCell,Mutex,RwLock,I've only found the example for constant with the Mutex in one of the docstrings:
I would like to be able to provide particular component wrapped for example by
Mutexand then be able to retrieve thisMutexfrom the container.I've created small example with comments what I would like to be able to achieve: