Skip to main content

ScopedContainer

Struct ScopedContainer 

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

A container with an associated scope identifier.

Useful for request-scoped or session-scoped services where you need to track the scope identity.

§Examples

use dependency_injector::{Container, ScopedContainer};

#[derive(Clone)]
struct RequestContext {
    request_id: String,
}

let root = Container::new();

// Create a request-scoped container
let request = ScopedContainer::from_parent(&root);
request.singleton(RequestContext {
    request_id: "req-123".to_string(),
});

let ctx = request.get::<RequestContext>().unwrap();
assert_eq!(ctx.request_id, "req-123");

Implementations§

Source§

impl ScopedContainer

Source

pub fn new() -> Self

Create a new scoped container with no parent.

Source

pub fn from_parent(parent: &Container) -> Self

Create a scoped container from a parent container.

Source

pub fn from_scope(parent: &ScopedContainer) -> Self

Create a scoped container from another scoped container.

Source

pub fn scope(&self) -> Scope

Get the scope identifier.

Source

pub fn singleton<T: Injectable>(&self, instance: T)

Register a singleton in this scope.

Source

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

Register a lazy singleton in this scope.

Source

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

Register a transient service in this scope.

Source

pub fn register<T: Injectable>(&self, instance: T)

Alias for singleton - register an instance.

Source

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

Register using a factory.

Source

pub fn get<T: Injectable>(&self) -> Result<Arc<T>>

Resolve a service from this scope or parent scopes.

Source

pub fn resolve<T: Injectable>(&self) -> Result<Arc<T>>

Alias for get.

Source

pub fn try_get<T: Injectable>(&self) -> Option<Arc<T>>

Try to resolve a service, returning None if not found.

Source

pub fn try_resolve<T: Injectable>(&self) -> Option<Arc<T>>

Alias for try_get.

Source

pub fn contains<T: Injectable>(&self) -> bool

Check if a service exists in this scope or parent scopes.

Source

pub fn has<T: Injectable>(&self) -> bool

Alias for contains.

Source

pub fn container(&self) -> &Container

Get the underlying container.

Source

pub fn container_mut(&mut self) -> &mut Container

Get the underlying container mutably.

Source

pub fn depth(&self) -> u32

Get the scope depth.

Trait Implementations§

Source§

impl Debug for ScopedContainer

Source§

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

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

impl Default for ScopedContainer

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> Injectable for T
where T: Send + Sync + 'static,

Source§

fn type_id_of() -> TypeId
where Self: Sized,

Returns the TypeId of this type (for internal use)
Source§

fn type_name_of() -> &'static str
where Self: Sized,

Returns the type name for debugging
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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
Source§

impl<R, D> DepsPresent<D> for R

Source§

impl<T> Provider for T
where T: Injectable,

Source§

impl<R, D> VerifyDeps<D> for R