pub trait IServiceResolverExt {
// Required method
fn resolve_arc<T: ?Sized + Send + Sync + 'static>(
&self,
) -> Result<Arc<T>, RdiError>;
}Expand description
Typed resolution helpers for factory closures.
Factories are passed &dyn IServiceResolver; the generic methods on
IServiceResolver cannot be called on the trait object, so this extension
restores type safety inside factories (no stringified type names, no manual
downcast). Bring it into scope with use rust_dix::IServiceResolverExt as _;
(or use rust_dix::*;).
Sync factory closures return Arc<T>, so ? belongs in a fallible helper
(or use .unwrap()/.expect()); the helper result is then wrapped with
inject_arc to keep the typed error:
use rust_dix::*;
use std::sync::Arc;
struct Config;
struct Handler {
config: Arc<Config>,
}
fn build_handler(resolver: &dyn IServiceResolver) -> Result<Arc<Handler>, RdiError> {
let config: Arc<Config> = resolver.resolve_arc()?; // `?` works here
Ok(Arc::new(Handler { config }))
}
let collection = ServiceCollection::new()
.singleton(|_| Arc::new(Config))
.singleton(|r| inject_arc(build_handler(r)));
let _ = collection;Required Methods§
Sourcefn resolve_arc<T: ?Sized + Send + Sync + 'static>(
&self,
) -> Result<Arc<T>, RdiError>
fn resolve_arc<T: ?Sized + Send + Sync + 'static>( &self, ) -> Result<Arc<T>, RdiError>
Resolve T (concrete or dyn Trait) as a shared instance.
Intended for factory closures — the only place where a resolver is seen
as &dyn IServiceResolver. On a container (ServiceProvider, Scope)
prefer the inherent get/get_optional, which are the primary API and
avoid the extra dyn-aware path.
Returns Err(RdiError::ServiceNotFound) when nothing is registered.
Services that are registered but not resolvable at this point
(strict-scope validation, disposed container/scope, circular dependency)
are also reported as Err — the typed RdiError payload of the
IServiceResolver::get_by_type_id panic contract is caught and
converted, so this method never panics for framework-level failures.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".