Crates.io | silhouette |
lib.rs | silhouette |
version | 0.1.0 |
source | src |
created_at | 2023-12-20 10:07:47.042362 |
updated_at | 2023-12-20 10:07:47.042362 |
description | A simple service container library for Rust |
homepage | |
repository | https://github.com/m1guelpf/silhouette |
max_upload_size | |
id | 1075162 |
size | 23,757 |
A simple dependency injection library for Rust
Silhouette implements a simple service container in Rust for dependency injection. It not only provides a Container
struct for local usage, but also a static interface (under facade::Container
) for easily managing dependencies throughout your application. It's heavily inspired by Laravel's Service Container.
use silhouette::facade::Container;
struct DBPool {}
struct DBConnection {}
// will always use the same pool
Container::singleton(&|_| DBPool::new())?;
// will resolve a new connection each time
Container::bind(&|container| -> DBConnection {
let shared_pool = container.resolve::<DBPool>().unwrap();
shared_pool.get_conn()
})?;
// somewhere else in your app...
let connection: DBConnection = Container::resolve()?;
Refer to the documentation on docs.rs for detailed usage instructions.
This project is licensed under the MIT License - see the LICENSE file for details.