2 unstable releases
new 0.2.0 | Oct 17, 2024 |
---|---|
0.1.0 | Oct 9, 2024 |
#441 in Development tools
342 downloads per month
61KB
615 lines
[dependencies]
ferrunix = "0"
Compiler support: requires rustc 1.64+
Changelog
Features
- Can register and inject any type (incl. generics, types must be
Send
+Sync
if themultithread
feature is enabled). - Simple and elegant Rust API, making the derive macro purely optional.
- Different dependency lifetimes:
- Singleton: Only a single instance of the object is created.
- Transient: A new instance is created for every request.
- Derive macro (
#[derive(Inject)]
) to simplify registration. - Automatic registration of types.
- One global registry; with support for multipiple sub-registries.
Usage
Add the dependency to your Cargo.toml
:
cargo add ferrunix
Register your types with the Registry
:
use ferrunix::{Ref, Registry, Transient};
use example::{Logger, BillingService, SysLog}
#[derive(Debug, Default)]
pub struct ExampleService {}
impl ExampleService {
pub fn do_work(&self) {
// Omitted for brevity...
}
}
fn main() {
let registry = Registry::global();
registry.transient(|| ExampleService::default());
// Register more types here ...
debug_assert!(registry.validate_all());
let service = registry.get_transient::<ExampleService>().unwrap();
service.do_work();
}
Features
Ferrunix has the following features to enable further functionality.
Default features are marked with *
.
multithread
(*
): Enable support for access to the registry from multiple threads. This adds a bound that all registered types must beSend
andSync
.derive
(*
): Enable support for the#[derive(Inject)]
macro.
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.By contributing to this project (for example, through submitting a pull request) you agree with the individual contributor license agreement. Make sure to read and understand it.
Dependencies
~0.7–5.5MB
~29K SLoC