Skip to main content

ServiceRegistry

Struct ServiceRegistry 

Source
pub struct ServiceRegistry { /* private fields */ }
Expand description

Typed map keyed by TypeId. Stores Arc<dyn Any + Send + Sync> per slot; clones on lookup (cheap — services are Arc internally). Built at boot, read-only thereafter.

Look up with exactly the type you registered. The key is TypeId::of::<T>() of the value passed to register, so registering an Arc<X> stores it under Arc<X>, and a later get::<X>() silently returns None. The convention for an already-shared handle is a named alias (type XHandle = Arc<X>) used on both sides — see BufferStoreHandle, ModeRegistryHandle and the other *Handle aliases in this crate.

§Examples

use std::sync::Arc;
use lattice_mode::ServiceRegistry;

struct Clock(u64);
type ClockHandle = Arc<Clock>;

let mut services = ServiceRegistry::new();
services.register::<ClockHandle>(Arc::new(Clock(42)));

// Same type on both sides: found (an `Arc<ClockHandle>`, i.e. two layers).
assert_eq!(services.get::<ClockHandle>().unwrap().0, 42);
// The TypeId pitfall: `Clock` was never registered, only `Arc<Clock>`.
assert!(services.get::<Clock>().is_none());

Implementations§

Source§

impl ServiceRegistry

Source

pub fn new() -> Self

An empty registry. The host fills it at boot; tests build one with only the services the code under test reads.

Source

pub fn register<T: Any + Send + Sync>(&mut self, service: T)

Register a service. Overwrites if a service of the same type was already registered (last-write-wins — typically called once per type at boot).

Source

pub fn get<T: Any + Send + Sync>(&self) -> Option<Arc<T>>

Look up a service by type. Returns a fresh Arc<T> clone when found; None when no service of that type is registered (calling renderer hasn’t wired this subsystem yet, or the mode is running in a stripped-down test harness).

Trait Implementations§

Source§

impl Debug for ServiceRegistry

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ServiceRegistry

Source§

fn default() -> ServiceRegistry

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more