rscontainer

Crates.iorscontainer
lib.rsrscontainer
version0.1.0
sourcesrc
created_at2021-06-17 13:51:41.277388
updated_at2021-06-17 13:51:41.277388
descriptionManages dependencies between objects.
homepagehttps://github.com/yvesdum/rscontainer
repositoryhttps://github.com/yvesdum/rscontainer
max_upload_size
id411366
size79,063
Yves D (yvesdum)

documentation

README

rscontainer

rscontainer is a library for the Rust programming language to manage dependencies between objects. The main type is the ServiceContainer, which serves two purposes: it acts as a registry for shared instances (singletons) and custom constructors, and it provides a mechanism for dependency injection.

For more information see the documentation.

Resolving instances

There are different kind of instances:

  • Owned instances: a fresh instance to be used in a owned scope. This instance will not be stored in the service container, you will get a new instance each time you resolve a owned instance.
  • Shared instances: an instance behind a smart pointer that is stored in the service container. You will get the same instance each time you resolve a shared service.
  • Some instances: an enum over owned and shared instances. Use this in a type when you want the user of your type to decide what kind of instance they want to supply.

How to use

Resolving a owned instance:

use rscontainer::ServiceContainer;

let mut container = ServiceContainer::new();
let mut foo = container.resolver().owned::<SomeService>(())?;
foo.do_something();

Resolving a shared instance (singleton):

use rscontainer::{ServiceContainer, Shared};

let mut container = ServiceContainer::new();
let foo: Shared<SomeService> = container.resolver().shared()?;

foo.access_mut(|foo| {
    let foo = foo.assert_healthy();
    foo.do_something();
});
Commit count: 48

cargo fmt