Skip to main content

Injectable

Trait Injectable 

Source
pub trait Injectable:
    Sized
    + Send
    + Sync
    + 'static {
    type Provider: Provider<Self>;

    const IS_SINGLETON: bool = true;
}
Expand description

The primary trait for types that can be dependency-injected.

§Derivable

This trait is normally derived via #[derive(Injectable)]:

#[derive(Injectable)]
pub struct Database {
    pool_size: usize,
}

The derive macro generates:

  • A <Type>Provider struct implementing Provider<Type>
  • All necessary Extract calls for constructor parameters
  • Lifecycle hook invocation (post_construct, pre_destruct)

§Associated Types

  • Provider: The compile-time generated provider that knows how to construct this type, including all its transitive dependencies.

§Bounds

All injectable types must be Send + Sync + 'static to support async construction and shared access across threads.

Provided Associated Constants§

Source

const IS_SINGLETON: bool = true

Whether this type is singleton-scoped (constructed once and cached).

Defaults to true. Transient-scoped types generated by the macro override this to false so the cache is bypassed on every resolution.

Required Associated Types§

Source

type Provider: Provider<Self>

The provider type that knows how to construct Self.

Generated by the #[derive(Injectable)] macro. Each provider statically encodes its dependency tree through Extract calls.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§