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
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(())}
The text was updated successfully, but these errors were encountered:
Mutex<Svc<dyn Foo>> (or other similar interior mutability types) is probably not what you want. In this case, you'd be guaranteeing mutually exclusive access to the shared pointer to the data, not to the data itself. This wouldn't give you access to call &mut self methods in dyn Foo. Instead, you probably want a Svc<Mutex<dyn Foo>> to be returned by the injector (for example).
I think this should be doable with new providers like you mentioned. An .in_mutex/.in_rc/etc. suite of extensions might be nice to have. Personally I think implementations of the interface should have interior mutability instead (meaning Foo::increase would instead take &self and FooImpl would hold an AtomicU64, for example), but I'm sure there are use cases where having a mutex/refcell/etc provided for you would be nice.
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
,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
Mutex
and then be able to retrieve thisMutex
from the container.I've created small example with comments what I would like to be able to achieve:
The text was updated successfully, but these errors were encountered: