16 releases

0.3.4 Jun 15, 2024
0.3.3 Jun 15, 2024
0.2.8 Jun 15, 2024
0.1.2 Jun 11, 2024

#411 in Procedural macros

Download history 56/week @ 2024-07-29 14/week @ 2024-09-23 2/week @ 2024-09-30

352 downloads per month

MIT license

18KB
380 lines

Rust Macro Dependency Injection

Import lib

rs-macro-di = "0.2.0"

or add with cargo:

cargo add rs-macro-di

Add dependency

// Add singleton service
add_singleton!(Arc::new(SingletonService::new()));

// Add scoped service
add_scoped!(ScopedService);

// Add transient service
add_transient!(ScopedService);

Use a configured service

let service: Arc<ServiceClass> = provide!(ServiceClass);

Or without macro

// Get provider
let mut provider = provider!();
// or
let mut provider = get_mut_service_provider();


// Add singleton service
provider.add_singleton(Arc::new(SingletonService::new()));

// Add scoped service
provider.add_scoped::<ScopedService>();

// Add transient service
provider.add_transient::<ScopedService>();
// Get provider
let provider = provider!();

let service: Arc<ServiceClass> = provider.provide::<ServiceClass>();

Use a scoped block

// Add scoped service
add_scoped!(ScopedService);

// scoped service instances are cleared at the end of with_scope
with_scope(|| {
    let service: Arc<ScopedService> = provide!(ScopedService);
})

Dependencies

~3MB
~62K SLoC