kizuna 0.1.0

A simple service locator
Documentation
  • Coverage
  • 85.71%
    18 out of 21 items documented1 out of 4 items with examples
  • Size
  • Source code size: 24.82 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 592.59 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 9s Average build duration of successful builds.
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Neo-Ciber94/kizuna
    1 0 1
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Neo-Ciber94

kizuna

CI-badge Latest Version Docs

🔍 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

cargo add kizuna

or in your Cargo.toml

kizuna = "0.1.0"

Usage

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::<Name>().unwrap()));

    let person = locator.get::<Person>().unwrap();
    assert_eq!(person.0, Name("Athena"));

    // You can call a function injecting the dependencies
    locator.invoke(greet).unwrap();
}

Support for async/await

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