[][src]Struct shaku::Container

pub struct Container { /* fields omitted */ }

Resolves services registered during the build phase.

A Container stores a single instance of each component, and stores provider functions. These component instances are made at container build time, during ContainerBuilder::build.

Examples

use std::sync::Arc;

use shaku::{Component, Interface};

trait FooValue: Interface {
    fn get_value(&self) -> usize;
    fn set_value(&mut self, _: usize);
}

#[derive(Component)]
#[shaku(interface = FooValue)]
struct FooImpl {
    value: usize,
}

impl FooValue for FooImpl {
    fn get_value(&self) -> usize {
        self.value
    }

    fn set_value(&mut self, val: usize) {
        self.value = val;
    }
}

let mut builder = shaku::ContainerBuilder::new();
builder
    .register_type::<FooImpl>()
    .with_named_parameter("value", 17 as usize);

let mut container = builder.build().unwrap();

{
    let foo: &dyn FooValue = container.resolve_ref().unwrap();
    assert_eq!(foo.get_value(), 17);
}

{
    let foo: &mut dyn FooValue = container.resolve_mut().unwrap();
    assert_eq!(foo.get_value(), 17);
    foo.set_value(99);
}

{
    let foo: Arc<dyn FooValue> = container.resolve().unwrap();
    assert_eq!(foo.get_value(), 99);
}

{
    let foo = container.resolve_ref::<dyn FooValue>().unwrap();
    assert_eq!(foo.get_value(), 99);
}

{
    let foo = container.resolve_mut::<dyn FooValue>().unwrap();
    assert_eq!(foo.get_value(), 99);
}

See also the module documentation for more details.

Methods

impl Container[src]

pub fn resolve<I: Interface + ?Sized>(&self) -> Result<Arc<I>>[src]

Get a reference to the component registered with the interface I. The ownership of the component is shared via Arc.

Errors

Returns a Error::ResolveError if the component is not found (most likely it wasn't registered)

Examples

let foo: Arc<dyn Foo> = container.resolve::<dyn Foo>().unwrap();

pub fn provide<I: ProvidedInterface + ?Sized>(&self) -> Result<Box<I>>[src]

Create a service using the provider registered with the interface I. Each call will create a new instance of the service.

Errors

Returns a Error::ResolveError if the provider is not found, or if the provider failed while creating the service.

Examples

let foo: Box<dyn Foo> = container.provide::<dyn Foo>().unwrap();

pub fn resolve_ref<I: Interface + ?Sized>(&self) -> Result<&I>[src]

Get a reference to the component registered with the interface I.

Errors

Returns a Error::ResolveError if the component is not found (most likely it wasn't registered)

Examples

let foo: &dyn Foo = container.resolve_ref::<dyn Foo>().unwrap();

pub fn resolve_mut<I: Interface + ?Sized>(&mut self) -> Result<&mut I>[src]

Get a mutable reference to the component registered with the interface I.

Errors

Returns a Error::ResolveError if the component is not found (most likely your component wasn't registered)

If the component is jointly owned (an Arc<I> reference to the component exists outside of this container), then a Error::ResolveError will be returned as it is unsafe to take a mutable reference without exclusive ownership of the component.

Examples

let foo: &mut dyn Foo = container.resolve_mut::<dyn Foo>().unwrap();

Trait Implementations

impl Debug for Container[src]

Auto Trait Implementations

Blanket Implementations

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

impl<T> Any for T where
    T: Any
[src]

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

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

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

impl<T> Interface for T where
    T: Send + Sync + Any
[src]

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

impl<T> ProvidedInterface for T where
    T: Send + Any
[src]

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.

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.