pub struct Scope { /* private fields */ }Expand description
A scoped service provider — created via ServiceProvider::scope.
内部持有一个 scope 范围的 ServiceProvider(由 ServiceProvider::new_scope 派生),
共享 root 的 store/singleton_cache/named,独立 root_scoped_cache。
所有解析方法委托给内嵌的 scope_provider,确保:
- Scoped 服务在 scope 内缓存复用(命中
scope_provider.root_scoped_cache)。 - Transient 依赖通过
scope_provider解析,正确绑定到当前 scope(MEDI 语义)。 #[inject(provider)]字段拿到的是 scope 范围的 provider,经该字段解析 Scoped 服务命中 scope 缓存——不泄漏到 root。
§资源释放
当 Scope 被 drop 时,其内部的 scope_provider 被释放,scoped 缓存中的
Arc<T> 实例随之释放。若服务需要确定性资源释放(如关闭数据库连接),
可在工厂内通过 ServiceProvider::register_disposable 注册清理闭包,
并调用 Scope::dispose 在 scope 结束前执行显式清理。
Implementations§
Source§impl Scope
impl Scope
Sourcepub fn scope_provider(&self) -> &Arc<ServiceProvider> ⓘ
pub fn scope_provider(&self) -> &Arc<ServiceProvider> ⓘ
Returns the scope-local ServiceProvider backing this scope.
Exposed for advanced use cases (e.g. passing the scope provider to
ServiceProviderWrapper). Most callers should use Self::get /
Self::get_owned directly.
Sourcepub fn dispose(&self)
pub fn dispose(&self)
Explicitly dispose all scoped services registered via ServiceProvider::register_disposable.
Services that need deterministic cleanup (e.g., database connections, file handles) should register a cleanup closure in their factory:
.scoped(|r| {
let conn = Arc::new(DbConnection::connect());
let conn_clone = Arc::clone(&conn);
r.provider_arc().unwrap().register_disposable(Box::new(move || {
conn_clone.close();
}));
conn
})Called automatically on Drop as a safety net. If Drop runs while a
concurrent resolution is in progress (factory holding the disposable
lock), disposal is skipped — some cleanups may be delayed or dropped.
For deterministic cleanup, call this explicitly before the scope goes
out of scope.
pub fn get<T: ?Sized + Send + Sync + 'static>(&self) -> Result<Arc<T>, RdiError>
pub fn get_optional<T: ?Sized + Send + Sync + 'static>(&self) -> Option<Arc<T>>
pub fn get_keyed<T: ?Sized + Send + Sync + 'static>( &self, key: &str, ) -> Result<Arc<T>, RdiError>
Sourcepub async fn get_async<T: ?Sized + Send + Sync + 'static>(
&self,
) -> Result<Arc<T>, RdiError>
pub async fn get_async<T: ?Sized + Send + Sync + 'static>( &self, ) -> Result<Arc<T>, RdiError>
Resolve an async service (transient or scoped) within this scope.
Sourcepub async fn get_keyed_async<T: ?Sized + Send + Sync + 'static>(
&self,
key: &str,
) -> Result<Arc<T>, RdiError>
pub async fn get_keyed_async<T: ?Sized + Send + Sync + 'static>( &self, key: &str, ) -> Result<Arc<T>, RdiError>
Resolve an async keyed service within this scope.
Sourcepub fn get_owned<T: Send + Sync + 'static>(&self) -> Result<T, RdiError>
pub fn get_owned<T: Send + Sync + 'static>(&self) -> Result<T, RdiError>
Resolve owned T within this scope (bypass cache, fresh each call).
Returns Err if not registered or the service is Singleton.
Sourcepub fn try_get_owned<T: Send + Sync + 'static>(&self) -> Option<T>
pub fn try_get_owned<T: Send + Sync + 'static>(&self) -> Option<T>
Resolve owned T, returning None if not registered or Singleton.
Sourcepub fn get_keyed_owned<T: Send + Sync + 'static>(
&self,
key: &str,
) -> Result<T, RdiError>
pub fn get_keyed_owned<T: Send + Sync + 'static>( &self, key: &str, ) -> Result<T, RdiError>
Resolve owned keyed T.
Returns Err if not found or Singleton.
Sourcepub fn try_get_keyed_owned<T: Send + Sync + 'static>(
&self,
key: &str,
) -> Option<T>
pub fn try_get_keyed_owned<T: Send + Sync + 'static>( &self, key: &str, ) -> Option<T>
Resolve owned keyed T, returning None if not found or Singleton.
pub fn get_all<T: ?Sized + Send + Sync + 'static>(&self) -> Vec<Arc<T>>
pub fn get_named_any(&self, name: &str) -> Option<Arc<dyn Any + Send + Sync>>
Sourcepub fn rdi_register_named(
&self,
name: &str,
service: Arc<dyn Any + Send + Sync>,
)
pub fn rdi_register_named( &self, name: &str, service: Arc<dyn Any + Send + Sync>, )
Register a named service (for IProvider trait).
Sourcepub fn rdi_remove_named(&self, name: &str)
pub fn rdi_remove_named(&self, name: &str)
Remove a named service (for IProvider trait).
Trait Implementations§
Source§impl IServiceResolver for Scope
impl IServiceResolver for Scope
fn get_any(&self, key: &str) -> Option<Arc<dyn Any + Send + Sync>>
fn get_keyed_any( &self, key: &str, variant: &str, ) -> Option<Arc<dyn Any + Send + Sync>>
Source§fn get_keyed_by_type_id(
&self,
tid: TypeId,
key: &str,
) -> Option<Arc<dyn Any + Send + Sync>>
fn get_keyed_by_type_id( &self, tid: TypeId, key: &str, ) -> Option<Arc<dyn Any + Send + Sync>>
None)。Source§fn get_all_any(&self, key: &str) -> Vec<Arc<dyn Any + Send + Sync>>
fn get_all_any(&self, key: &str) -> Vec<Arc<dyn Any + Send + Sync>>
Source§fn get_owned_any(&self, key: &str) -> Option<Arc<dyn Any + Send + Sync>>
fn get_owned_any(&self, key: &str) -> Option<Arc<dyn Any + Send + Sync>>
Arc<dyn Any + Send + Sync> (concrete Arc<Arc<T>>). Read morefn get_keyed_owned_any( &self, key: &str, variant: &str, ) -> Option<Arc<dyn Any + Send + Sync>>
Source§fn provider_arc(&self) -> Option<Arc<ServiceProvider>>
fn provider_arc(&self) -> Option<Arc<ServiceProvider>>
Arc<ServiceProvider> for on-demand service resolution. Read moreSource§fn get<T: ?Sized + Sync + Send + 'static>(&self) -> Result<Arc<T>, RdiError>where
Self: Sized,
fn get<T: ?Sized + Sync + Send + 'static>(&self) -> Result<Arc<T>, RdiError>where
Self: Sized,
dyn Trait).
Returns Err(RdiError::ServiceNotFound) if not registered. Read moreSource§fn try_get<T: ?Sized + Sync + Send + 'static>(&self) -> Option<Arc<T>>where
Self: Sized,
fn try_get<T: ?Sized + Sync + Send + 'static>(&self) -> Option<Arc<T>>where
Self: Sized,
None if not registered.Source§fn get_keyed<T: ?Sized + Sync + Send + 'static>(
&self,
variant: &str,
) -> Result<Arc<T>, RdiError>where
Self: Sized,
fn get_keyed<T: ?Sized + Sync + Send + 'static>(
&self,
variant: &str,
) -> Result<Arc<T>, RdiError>where
Self: Sized,
Err if not found.Source§fn try_get_keyed<T: ?Sized + Sync + Send + 'static>(
&self,
variant: &str,
) -> Option<Arc<T>>where
Self: Sized,
fn try_get_keyed<T: ?Sized + Sync + Send + 'static>(
&self,
variant: &str,
) -> Option<Arc<T>>where
Self: Sized,
None if not found.Source§fn get_all<T: ?Sized + Sync + Send + 'static>(&self) -> Vec<Arc<T>>where
Self: Sized,
fn get_all<T: ?Sized + Sync + Send + 'static>(&self) -> Vec<Arc<T>>where
Self: Sized,
T (default + keyed). Read moreSource§fn get_owned<T: Send + Sync + 'static>(&self) -> Result<T, RdiError>where
Self: Sized,
fn get_owned<T: Send + Sync + 'static>(&self) -> Result<T, RdiError>where
Self: Sized,
T (bypass cache, fresh each call).
Returns Err if not registered or the service is Singleton.Source§fn try_get_owned<T: Send + Sync + 'static>(&self) -> Option<T>where
Self: Sized,
fn try_get_owned<T: Send + Sync + 'static>(&self) -> Option<T>where
Self: Sized,
T, returning None if not registered or Singleton.