ScopePool

Struct ScopePool 

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

A pool of pre-allocated scopes for high-throughput scenarios.

Creating a scope involves allocating a DashMap (~134ns). For web servers handling thousands of requests per second, this adds up. ScopePool pre-allocates scopes and reuses them, reducing per-request overhead to near-zero.

§Example

use dependency_injector::{Container, ScopePool};

#[derive(Clone)]
struct AppConfig { name: String }

#[derive(Clone)]
struct RequestId(String);

// Create root container with app-wide services
let root = Container::new();
root.singleton(AppConfig { name: "MyApp".into() });

// Create a pool of reusable scopes (pre-allocates 4 scopes)
let pool = ScopePool::new(&root, 4);

// In request handler: acquire a pooled scope
{
    let scope = pool.acquire();
    scope.singleton(RequestId("req-123".into()));

    // Can access parent services
    assert!(scope.contains::<AppConfig>());
    assert!(scope.contains::<RequestId>());

    // Scope automatically released when dropped
}

// Next request reuses the same scope allocation
{
    let scope = pool.acquire();
    // Previous RequestId is cleared, fresh scope
    assert!(!scope.contains::<RequestId>());
}

§Performance

  • First acquisition: ~134ns (creates new scope if pool is empty)
  • Subsequent acquisitions: ~20ns (reuses pooled scope)
  • Release: ~10ns (clears and returns to pool)

Implementations§

Source§

impl ScopePool

Source

pub fn new(parent: &Container, capacity: usize) -> Self

Create a new scope pool with pre-allocated capacity.

§Arguments
  • parent - The parent container that scopes will inherit from
  • capacity - Number of scopes to pre-allocate
§Example
use dependency_injector::{Container, ScopePool};

let root = Container::new();
// Pre-allocate 8 scopes for concurrent request handling
let pool = ScopePool::new(&root, 8);
Source

pub fn acquire(&self) -> PooledScope<'_>

Acquire a scope from the pool.

Returns a PooledScope that automatically returns to the pool when dropped. If the pool is empty, creates a new scope.

§Example
use dependency_injector::{Container, ScopePool};

#[derive(Clone)]
struct RequestData { id: u64 }

let root = Container::new();
let pool = ScopePool::new(&root, 4);

let scope = pool.acquire();
scope.singleton(RequestData { id: 123 });
let data = scope.get::<RequestData>().unwrap();
assert_eq!(data.id, 123);
Source

pub fn available_count(&self) -> usize

Get the current number of available scopes in the pool.

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