Crates.io | portaldi |
lib.rs | portaldi |
version | 0.2.16 |
source | src |
created_at | 2023-01-07 04:37:54.189843 |
updated_at | 2024-07-19 08:49:32.326589 |
description | An ergonomic lightweight compile-time depencency injection library. |
homepage | |
repository | https://github.com/mtn81/portaldi |
max_upload_size | |
id | 752779 |
size | 35,451 |
PortalDI is a ergonomic, lightweight and compile-time dependency injection (DI) library for Rust.
Ergonomic apis for DI.
You can forcus on a target type in most cases without worrying about containers.
Hoge::di().hello();
Natively async support.
AsyncHoge::di()
.await
.hello()
DRY support by proc-macros.
Almost boiler codes can be generated by PortalDI's proc-macro.
#[derive(DIPortal)]
struct Hoge {
foo: DI<Foo>,
...
}
Wasm support.
In Wasm target, PortalDI compiles DI container as Rc compatible.
In non Wasm target, components and traits must be thread-safe
(Sync + Send
).
use portaldi::*;
#[derive(DIPortal)]
struct Hoge {
foo: DI<dyn FooI>,
bar: DI<Bar>,
}
impl Hoge {
fn say_hello(&self) {
println!("hello hoge < {}, {}", self.foo.hello(), self.bar.hello())
}
}
pub trait FooI: DITarget {
fn hello(&self) -> &str;
}
#[derive(DIPortal)]
#[provide(FooI)]
struct Foo {}
impl FooI for Foo {
fn hello(&self) -> &str {
"hello foo"
}
}
#[derive(DIPortal)]
struct Bar {}
impl Bar {
fn hello(&self) -> &str {
"hello bar"
}
}
fn main() {
Hoge::di().say_hello();
}
For detailed guides, see docs page
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.