Skip to main content

Container

Struct Container 

Source
pub struct Container { /* private fields */ }
Expand description

A registry of services keyed by their Rust type.

Cheap to share: put it in an Arc and clone the handle. All methods take &self — bindings are registered through interior mutability, because service providers register into a container that is already shared.

Implementations§

Source§

impl Container

Source

pub fn new() -> Self

An empty container.

Source

pub fn bind<T, F>(&self, factory: F)
where T: Send + Sync + 'static, F: Fn(&Container) -> Result<T> + Send + Sync + 'static,

Bind a factory that runs on every resolution.

Source

pub fn singleton<T, F>(&self, factory: F)
where T: Send + Sync + 'static, F: Fn(&Container) -> Result<T> + Send + Sync + 'static,

Bind a factory that runs once; every resolution afterwards hands back the same Arc.

Source

pub fn scoped<T, F>(&self, factory: F)
where T: Send + Sync + 'static, F: Fn(&Container) -> Result<T> + Send + Sync + 'static,

Like singleton, but flush_scoped drops the memoised instance so the next resolution rebuilds it.

This is the binding a long-running worker wants for per-job state: the queue worker flushes scoped bindings between jobs so one job cannot leak state into the next.

Source

pub fn instance<T: Send + Sync + 'static>(&self, value: T)

Register an already-built value. The container never constructs it.

Source

pub fn instance_arc<T: Send + Sync + 'static>(&self, value: Arc<T>)

Register an already-built value that is already behind an Arc — useful when the caller must keep a handle to it too.

Source

pub fn resolve<T: Send + Sync + 'static>(&self) -> Result<Arc<T>>

Resolve T, building it if necessary.

Fails when T was never bound, when its factory fails, or when resolving it re-enters itself (a dependency cycle).

Source

pub fn try_resolve<T: Send + Sync + 'static>(&self) -> Option<Arc<T>>

Resolve T, or None if it is unbound or its factory failed.

Source

pub fn expect_resolve<T: Send + Sync + 'static>(&self) -> Arc<T> ⓘ

Resolve T, panicking with the resolution error if it is not available. For bootstrap code and facades, where an unbound service is a configuration bug rather than a runtime condition.

Source

pub fn bound<T: 'static>(&self) -> bool

Whether anything is bound for T.

Source

pub fn forget<T: 'static>(&self)

Drop the binding for T, if any.

Source

pub fn flush_scoped(&self)

Drop the memoised instance of every scoped binding. The bindings survive; only the built values are discarded.

Source

pub fn len(&self) -> usize

How many types are bound.

Source

pub fn is_empty(&self) -> bool

Whether nothing is bound.

Trait Implementations§

Source§

impl Debug for Container

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Container

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more