mini_service_locator

Crates.iomini_service_locator
lib.rsmini_service_locator
version0.1.0
sourcesrc
created_at2023-05-12 19:59:42.379585
updated_at2023-05-12 19:59:42.379585
descriptionA simple Service Locator implementation.
homepagehttps://github.com/emctague/mini_service_locator
repositoryhttps://github.com/emctague/mini_service_locator
max_upload_size
id863256
size8,254
Ethan McTague (emctague)

documentation

README

mini_service_locator

Crates.io docs.rs GitHub Workflow Status Crates.io

mini_service_locator provides a simple thread-safe service locator: a container that stores "service" objects of different types, allowing for retrieval of a service by its type.

Example

use mini_service_locator::ServiceLocator;

struct MyUsefulService {
    some_shared_thing: i32
}

let mut locator = ServiceLocator::default();

// Put a MyUsefulService into the locator.
locator.provide(MyUsefulService { some_shared_thing: 24 });

// Later, we can fetch this service.
// If we *don't* use store the resulting service as `mut`, we won't be allowed to write to it.
// We can run get<MyUsefulService> multiple times, and it will always provide the same service.
let mut useful_service = locator.get::<MyUsefulService>().unwrap();

assert_eq!(useful_service.read().some_shared_thing, 24);
useful_service.write().some_shared_thing = 32;
assert_eq!(useful_service.read().some_shared_thing, 32);
Commit count: 1

cargo fmt