[−][src]Enum coi::Registration
Control when Container
will call Provide::provide
.
Variants
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 _ );
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 _ ); }
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]
fn clone(&self) -> Registration<T>
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Auto Trait Implementations
impl<T> RefUnwindSafe for Registration<T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
impl<T> Send for Registration<T> where
T: Send,
T: Send,
impl<T> Sync for Registration<T> where
T: Sync,
T: Sync,
impl<T> Unpin for Registration<T> where
T: Unpin,
T: Unpin,
impl<T> UnwindSafe for Registration<T> where
T: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,