Crates.io | jabba |
lib.rs | jabba |
version | 0.1.0 |
source | src |
created_at | 2023-05-09 22:46:11.795277 |
updated_at | 2023-05-09 22:46:11.795277 |
description | Async dependency injection framework |
homepage | |
repository | https://github.com/zortax/jabba |
max_upload_size | |
id | 860852 |
size | 28,705 |
Async dependency injection for Rust [WIP]
trait TestTrait: Injectable<Error = Infallible> + Send {
fn value(&self) -> usize;
}
struct TestStruct {
value: usize,
}
impl TestTrait for TestStruct {
fn value(&self) -> usize {
self.value
}
}
impl Injectable for TestStruct {
type Error = Infallible;
async fn create(_: Injector) -> Result<Self, Self::Error> {
static COUNTER: AtomicUsize = AtomicUsize::new(42);
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
Ok(TestStruct {
value: COUNTER.fetch_add(1, Ordering::SeqCst),
})
}
}
#[tokio::main]
async fn main() {
let injector = Injector::new();
injector.bind::<dyn TestTrait, TestStruct>().await;
let instance = injector.get::<dyn TestTrait>().await.unwrap();
println!("instance value: {}", instance1.value());
}