#service #locator #mini #i32 #store #object #write

mini_service_locator

一个简单的服务定位器实现

1 个不稳定版本

0.1.0 2023年5月12日

#6 in #locator

MIT 许可证

6KB

mini_service_locator

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

mini_service_locator 提供一个简单的线程安全的服务定位器:一个存储不同类型“服务”对象的容器,允许通过类型检索服务。

示例

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);

依赖项

~9KB