https://github.com/neo-ciber94/kizuna
A simple service locator
https://github.com/neo-ciber94/kizuna
Last synced: 8 months ago
JSON representation
A simple service locator
- Host: GitHub
- URL: https://github.com/neo-ciber94/kizuna
- Owner: Neo-Ciber94
- License: mit
- Created: 2023-04-18T21:08:56.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-04-19T02:11:54.000Z (over 2 years ago)
- Last Synced: 2025-02-06T17:57:06.617Z (8 months ago)
- Language: Rust
- Size: 18.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# kizuna
[![CI-badge]](ci) [![Latest Version]][crates.io] [![Docs][docs-badge]][docs-link]
[CI-badge]: https://github.com/Neo-Ciber94/kizuna/actions/workflows/ci.yml/badge.svg
[ci]: https://github.com/Neo-Ciber94/kizuna/actions/workflows/ci.yml[latest version]: https://img.shields.io/crates/v/kizuna.svg
[crates.io]: https://crates.io/crates/kizuna[docs-badge]: https://img.shields.io/badge/docs-kizuna-blue.svg
[docs-link]: https://docs.rs/kizuna/latest🔍 A simple service locator for Rust.
This library provides a simple service locator for Rust programs. It allows for easy insertion and retrieval of values by type, and supports both single instance values and values created by factory functions.
## Add to your project
```bash
cargo add kizuna
```or in your `Cargo.toml`
```toml
kizuna = "0.1.0"
```## Usage
```rust
use kizuna::Locator;#[derive(Debug, Clone, PartialEq)]
struct Name(&'static str);#[derive(Debug)]
struct Person(Name);fn greet(person: Person) {
println!("Hello {:?}", person.0);
}fn main() {
let mut locator = Locator::new();
// Register the dependencies
locator.insert(Name("Athena"));
locator.insert_with(|locator| Person(locator.get::().unwrap()));let person = locator.get::().unwrap();
assert_eq!(person.0, Name("Athena"));// You can call a function injecting the dependencies
locator.invoke(greet).unwrap();
}
```## Support for `async/await`
```rust
use kizuna::Locator;async fn hello(greet: String) -> usize {
println!("{greet}");
greet.len()
}#[tokio::main]
async fn main() {
let mut locator = Locator::new();
locator.insert(String::from("hello world"));let result = locator.invoke_async(hello).await.unwrap();
assert_eq!(result, 11);
}
```## Test
Run tests with `cargo test --lib`
## License
This project is licensed under the MIT License