Derive Macro DIPortal

Source
#[derive(DIPortal)]
{
    // Attributes available to this derive:
    #[provide]
    #[inject]
}
Expand description

Generate a DIPortal and [DIProvider] or [AsyncDIPortal] and [AsyncDIProvider] implementation.

  • provide: generate [DIProvider] implementation for a specified trait.

    #[derive(DIPortal)]
    #[provide(HogeI)] // HogeIProvider will be generated.
    struct Hoge {
      foo: DI<dyn FooI>  // needs FooIProvider in the current scope.
    }

    For a trait with generics,

    #[derive(DIPortal)]
    #[provide(HogeI<A>)] // HogeIAProvider will be generated.
    struct Hoge {
      foo: DI<dyn FooI<B>>  // needs FooIBProvider in the current scope.
    }
  • inject: specify DI settings for a field.

    #[derive(DIPortal)]
    struct Hoge {
      #[inject(Foo)]  // specify concrete type
      foo: DI<dyn FooI>,
      #[inject(AsyncFoo, async)]  // specify concrete type that needs async creation,
                                  // and consequently AsyncDIPortal for Hoge will be generated.
      foo2: DI<dyn FooI>,
      #[inject(async)]  // Bar nedds async creation,
                        // and consequently AsyncDIPortal for Hoge will be generated.
                        // implicitly BarProvider is used.
      bar: DI<Bar>,
      #[inject(MyBazProvider)] // specify DI provider for a another crate concrete type.
      baz: DI<Baz>,
      baz2: DI<Baz>,            // implicitly BarProvider is used.
      piyo: DI<dyn IPiyo>,      // implicitly IPiyoProvider is used.
      piyo2: DI<dyn IPiyo2<A>>, // implicitly IPiyo2AProvider is used.
    }