[][src]Enum coi::Registration

pub enum Registration<T> {
    Normal(T),
    Scoped(T),
    Singleton(T),
}

Control when Container will call Provide::provide.

Variants

Normal(T)

Container will construct a new instance of T for every invocation of Container::resolve.

Example

let mut container = container! {
    // same as trait => ImplProvider.normal
    trait => ImplProvider
};

let instance_1 = container.resolve::<dyn Trait>("trait").await?;
let instance_2 = container.resolve::<dyn Trait>("trait").await?;

// Every instance resolved from the container will be a distinct instance.
assert_ne!(
    instance_1.deref() as &dyn Trait as *const _,
    instance_2.deref() as &dyn Trait as *const _
);
Scoped(T)

Container will construct a new instance of T for each scope container created through Container::scoped.

Example

let mut container = container! {
    trait => ImplProvider.scoped
};

// Every instance resolved within the same scope will be the same instance.
let instance_1 = container.resolve::<dyn Trait>("trait").await?;
let instance_2 = container.resolve::<dyn Trait>("trait").await?;
assert_eq!(
    instance_1.deref() as &dyn Trait as *const _,
    instance_2.deref() as &dyn Trait as *const _
);
{
    let mut scoped = container.scopable().scoped().await;
    let instance_3 = scoped.resolve::<dyn Trait>("trait").await?;

    // Since these two were resolved in different scopes, they will never be the
    // same instance.
    assert_ne!(
        instance_1.deref() as &dyn Trait as *const _,
        instance_3.deref() as &dyn Trait as *const _
    );
}
Singleton(T)

The container will construct a single instance of T and reuse it throughout all scopes.

Example

let mut container = container! {
    trait => ImplProvider.singleton
};

let instance_1 = container.resolve::<dyn Trait>("trait").await?;
let instance_2 = container.resolve::<dyn Trait>("trait").await?;

assert_eq!(
    instance_1.deref() as &dyn Trait as *const _,
    instance_2.deref() as &dyn Trait as *const _
);
{
    let mut scoped = container.scopable().scoped().await;
    let instance_3 = scoped.resolve::<dyn Trait>("trait").await?;

    // Regardless of what scope the instance was resolved it, it will always
    // be the same instance.
    assert_eq!(
        instance_1.deref() as &dyn Trait as *const _,
        instance_3.deref() as &dyn Trait as *const _
    );
}

Trait Implementations

impl<T: Clone> Clone for Registration<T>[src]

Auto Trait Implementations

impl<T> RefUnwindSafe for Registration<T> where
    T: RefUnwindSafe

impl<T> Send for Registration<T> where
    T: Send

impl<T> Sync for Registration<T> where
    T: Sync

impl<T> Unpin for Registration<T> where
    T: Unpin

impl<T> UnwindSafe for Registration<T> where
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[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> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

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.