Struct runtime_injector::Services[][src]

pub struct Services<I: ?Sized + Interface> { /* fields omitted */ }
Expand description

A collection of all the providers for a particular interface.

If an interface will only have one implementation registered for it, then it may be easier to request Svc<T> from the container instead. However, if multiple implementations are registered (or no implementations are registered), then this will allow all of those implementations to be iterated over.

An iterator over all the implementations of an interface. Each service is activated on demand.

use runtime_injector::{
    interface, Injector, IntoTransient, Services, Svc, TypedProvider,
};

trait Fooable: Send + Sync {
    fn baz(&self) {}
}

interface!(dyn Fooable = [Foo, Bar]);

#[derive(Default)]
struct Foo;
impl Fooable for Foo {}

#[derive(Default)]
struct Bar;
impl Fooable for Bar {}

let mut builder = Injector::builder();
builder.provide(Foo::default.transient().with_interface::<dyn Fooable>());
builder.provide(Bar::default.transient().with_interface::<dyn Fooable>());

let injector = builder.build();
let mut counter = 0;
let mut fooables: Services<dyn Fooable> = injector.get().unwrap();
for foo in fooables.get_all() {
    counter += 1;
    foo.unwrap().baz();
}

assert_eq!(2, counter);

Implementations

impl<I: ?Sized + Interface> Services<I>[src]

pub fn get_all(&mut self) -> ServicesIter<'_, I>

Notable traits for ServicesIter<'a, I>

impl<'a, I: ?Sized + Interface> Iterator for ServicesIter<'a, I> type Item = InjectResult<Svc<I>>;
[src]

Lazily gets all the implementations of this interface. Each service will be requested on demand rather than all at once.

pub fn get_all_owned(&mut self) -> OwnedServicesIter<'_, I>

Notable traits for OwnedServicesIter<'a, I>

impl<'a, I: ?Sized + Interface> Iterator for OwnedServicesIter<'a, I> type Item = InjectResult<Box<I>>;
[src]

Lazily gets all the implementations of this interface as owned service pointers. Each service will be requested on demand rather than all at once. Not all providers can provide owned service pointers, so some requests may fail.

#[must_use]
pub fn len(&self) -> usize
[src]

Gets the max number of possible implementations of this interface. This does not take into account conditional providers, which may not return an implementation of the service.

#[must_use]
pub fn is_empty(&self) -> bool
[src]

Returns true if there are no possible implementations of this interface. This does not take into account conditional providers, which may not return an implementation of the service.

Trait Implementations

impl<I: ?Sized + Interface> Drop for Services<I>[src]

fn drop(&mut self)[src]

Executes the destructor for this type. Read more

impl<I: ?Sized + Interface> Request for Services<I>[src]

Lazily requests all the implementations of an interface.

fn request(injector: &Injector, info: &RequestInfo) -> InjectResult<Self>[src]

Performs the request to the injector.

Auto Trait Implementations

impl<I> !RefUnwindSafe for Services<I>

impl<I: ?Sized> Send for Services<I>

impl<I: ?Sized> Sync for Services<I>

impl<I: ?Sized> Unpin for Services<I>

impl<I> !UnwindSafe for Services<I>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

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]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.