Struct runtime_injector::Injector [−][src]
A runtime dependency injection container. This holds all the bindings between service types and their providers, as well as all the mappings from interfaces to their implementations (if they differ).
Implementations
impl Injector
[src]
#[must_use]pub fn builder() -> InjectorBuilder
[src]
Creates a build for this injector. This is the preferred way of creating an injector.
#[must_use]pub fn new(
providers: HashMap<ServiceInfo, Option<Box<dyn Provider>>>,
implementations: HashMap<ServiceInfo, ServiceInfo>
) -> Self
[src]
providers: HashMap<ServiceInfo, Option<Box<dyn Provider>>>,
implementations: HashMap<ServiceInfo, ServiceInfo>
) -> Self
Creates a new injector directly from its providers and implementations.
Prefer Injector::builder()
for creating new injectors instead.
pub fn get<R: Request>(&mut self) -> InjectResult<R>
[src]
Performs a request for a service. There are several types of requests that can be made to the service container by default:
Svc<I>
: Request a service pointer to the given interface and create an instance of the service if needed.Option<Svc<I>>
: Request a service pointer to the given interface and create an instance of the service if needed. If no provider for that service is registered, then returnOk(None)
rather than throwing an error.
Requests to service pointers of sized types will attempt to use the a registered provider to retrieve an instance of that service. For instance, a request for a singleton service will create an instance of that service if one doesn’t exist already, and either return a service pointer to the instance that was already created, or return a service pointer to the new instance (if one didn’t exist already).
use runtime_injector::{Injector, Svc, IntoSingleton}; #[derive(Default)] struct Bar; let mut builder = Injector::builder(); builder.provide(Bar::default.singleton()); let mut injector = builder.build(); let _bar: Svc<Bar> = injector.get().unwrap();
Requests to service pointers of dyn Trait
interface types will
instead request the implementation of that interface type. For example,
if dyn Foo
’s registered implementation is for the service type Bar
,
then a request for a service pointer of dyn Foo
will return a service
pointer to a Bar
, although the return type will be Svc<dyn Foo>
.
use runtime_injector::{interface, Injector, Svc, IntoSingleton}; trait Foo: Send + Sync {} interface!(Foo = [Bar]); #[derive(Default)] struct Bar; impl Foo for Bar {} let mut builder = Injector::builder(); builder.provide(Bar::default.singleton()); builder.implement::<dyn Foo, Bar>(); let mut injector = builder.build(); let _bar: Svc<dyn Foo> = injector.get().unwrap();
Custom request types can also be used by implementing Request
.
pub fn get_implementation(
&mut self,
interface: ServiceInfo
) -> Option<ServiceInfo>
[src]
&mut self,
interface: ServiceInfo
) -> Option<ServiceInfo>
Gets the service info for the registered implementation of a particular
interface. This is only used by dyn Trait
interface types to request
the registered implementation of that trait. For sized service types,
the implementation is always the type itself.
pub fn get_exact<T: Service>(&mut self) -> InjectResult<Svc<T>>
[src]
Gets an instance of the service with exactly the type that was
requested. This will not attempt to find the type registered as an
implementation of a particular trait. In fact, dynamic types (dyn Trait
) cannot be used with this function.
pub fn get_dyn_exact(
&mut self,
service_info: ServiceInfo
) -> InjectResult<DynSvc>
[src]
&mut self,
service_info: ServiceInfo
) -> InjectResult<DynSvc>
Similar to get_exact
, but returns an instance of dyn Any
instead,
and does not need the type passed in via a type parameter.
Auto Trait Implementations
impl !RefUnwindSafe for Injector
impl !Send for Injector
impl !Sync for Injector
impl Unpin for Injector
impl !UnwindSafe for Injector
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,