pub struct ServiceRegistry { /* private fields */ }Expand description
A generic service registry keyed by TypeId.
Services are stored as Arc<dyn Any + Send + Sync> and can be retrieved
by their concrete type via get.
§Example
use khora_core::service_registry::ServiceRegistry;
struct MyService { value: i32 }
let mut registry = ServiceRegistry::new();
registry.insert(MyService { value: 42 });
let svc = registry.get::<MyService>().unwrap();
assert_eq!(svc.value, 42);Implementations§
Source§impl ServiceRegistry
impl ServiceRegistry
Sourcepub fn insert<T: Send + Sync + 'static>(&mut self, service: T)
pub fn insert<T: Send + Sync + 'static>(&mut self, service: T)
Inserts a service into the registry, keyed by T’s TypeId.
If a service of the same type was already registered, it is replaced.
Sourcepub fn get<T: Send + Sync + 'static>(&self) -> Option<&T>
pub fn get<T: Send + Sync + 'static>(&self) -> Option<&T>
Retrieves a shared reference to a previously registered service.
Returns None if no service of type T has been registered.
Trait Implementations§
Source§impl Default for ServiceRegistry
impl Default for ServiceRegistry
Source§fn default() -> ServiceRegistry
fn default() -> ServiceRegistry
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for ServiceRegistry
impl !RefUnwindSafe for ServiceRegistry
impl Send for ServiceRegistry
impl Sync for ServiceRegistry
impl Unpin for ServiceRegistry
impl !UnwindSafe for ServiceRegistry
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more