[][src]Struct shaku::container::ContainerBuilder

pub struct ContainerBuilder { /* fields omitted */ }

Registers components in order to build a Container.

Once finished, call build to build the Container.

See module documentation or ContainerBuilder::build for more details.

Methods

impl ContainerBuilder[src]

pub fn new() -> Self[src]

Create a new ContainerBuilder.

pub fn register_type<C: Component>(&mut self) -> &mut ComponentRegistration[src]

Register a new component with this builder. If the component was already registered, the old component is replaced.

This method returns a mutable ComponentRegistration, allowing you to chain calls to with_named_parameter or with_typed_parameter.

pub fn register_lambda<I: Interface + ?Sized>(
    &mut self,
    component_name: &str,
    build: Box<dyn FnOnce(&mut ContainerBuildContext, &mut ParameterMap) -> Result<()>>,
    dependencies: Vec<Dependency>
) -> &mut ComponentRegistration
[src]

Register a new component with this builder. If the component was already registered, the old component is replaced.

This register method is an alternative to implementing Component. This may be useful in cases such as using a mock or dynamically choosing the implementation based on dependencies.

This method returns a mutable ComponentRegistration, allowing you to chain calls to with_named_parameter or with_typed_parameter.

pub fn register_provider<P: Provider + ?Sized>(&mut self)[src]

Register a new provider with this builder. If the provider was already registered, the old provider is replaced.

pub fn register_provider_lambda<I: ProvidedInterface + ?Sized>(
    &mut self,
    provider_name: &str,
    dependencies: Vec<Dependency>,
    provider: Box<dyn Fn(&Container) -> Result<Box<I>> + Send + Sync>
)
[src]

Register a new provider with this builder. If the provider was already registered, the old provider is replaced.

This register method is an alternative to implementing Provider. This may be useful in cases such as using a mock or dynamically choosing the implementation based on dependencies.

pub fn build(self) -> Result<Container>[src]

Consume this ContainerBuilder to build a Container. The ContainerBuildContext struct will be used to build the Container. The components are built at this time, and dependencies are checked.

Errors

The components are built at this time, so any dependency or parameter errors will be returned here.

Example

use shaku::{Component, Error as DIError, Interface};

trait Foo: Interface { fn foo(&self); }
trait FooDuplicate: Interface { fn foo(&self) -> String; }

#[derive(Component)]
#[shaku(interface = Foo)]
struct FooImpl;

#[derive(Component)]
#[shaku(interface = FooDuplicate)]
struct FooDuplicateImpl1;

#[derive(Component)]
#[shaku(interface = FooDuplicate)]
struct FooDuplicateImpl2;

impl Foo for FooImpl { fn foo(&self) { } }
impl FooDuplicate for FooDuplicateImpl1 { fn foo(&self) -> String { "FooDuplicateImpl1".to_string() } }
impl FooDuplicate for FooDuplicateImpl2 { fn foo(&self) -> String { "FooDuplicateImpl2".to_string() } }

{
    // Valid registration
    let mut builder = shaku::ContainerBuilder::new();
    builder.register_type::<FooImpl>();

    let container = builder.build();
    assert!(container.is_ok());
    let foo = container.unwrap().resolve::<dyn Foo>();
    assert!(foo.is_ok());
}

{
    // Duplicate registration, only the latest component registered is kept
    let mut builder = shaku::ContainerBuilder::new();
    builder.register_type::<FooDuplicateImpl1>();
    builder.register_type::<FooDuplicateImpl2>();

    let container = builder.build();
    assert!(container.is_ok());
    let mut container = container.unwrap();
    let foo = container.resolve::<dyn FooDuplicate>();
    assert!(foo.is_ok());
    assert_eq!(foo.unwrap().foo(), "FooDuplicateImpl2".to_string());
}

Trait Implementations

impl Default for ContainerBuilder[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, U> Into<U> for T where
    U: From<T>, 
[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.