diff options
Diffstat (limited to 'src/async_di_container.rs')
-rw-r--r-- | src/async_di_container.rs | 68 |
1 files changed, 41 insertions, 27 deletions
diff --git a/src/async_di_container.rs b/src/async_di_container.rs index ecf3a41..23eb2eb 100644 --- a/src/async_di_container.rs +++ b/src/async_di_container.rs @@ -406,48 +406,62 @@ impl AsyncDIContainer AsyncProvidable::Transient(transient_binding) => { Ok(SomeThreadsafePtr::Transient( transient_binding.cast::<Interface>().map_err(|_| { - AsyncDIContainerError::CastFailed(type_name::<Interface>()) + AsyncDIContainerError::CastFailed { + interface: type_name::<Interface>(), + binding_kind: "transient", + } })?, )) } AsyncProvidable::Singleton(singleton_binding) => { - Ok( - SomeThreadsafePtr::ThreadsafeSingleton( - singleton_binding.cast::<Interface>().map_err( - |err| match err { - CastError::NotArcCastable(_) => { - AsyncDIContainerError::InterfaceNotAsync(type_name::< - Interface, - >( - )) + Ok(SomeThreadsafePtr::ThreadsafeSingleton( + singleton_binding + .cast::<Interface>() + .map_err(|err| match err { + CastError::NotArcCastable(_) => { + AsyncDIContainerError::InterfaceNotAsync(type_name::< + Interface, + >( + )) + } + CastError::CastFailed { from: _, to: _ } => { + AsyncDIContainerError::CastFailed { + interface: type_name::<Interface>(), + binding_kind: "singleton", } - CastError::CastFailed { from: _, to: _ } => { - AsyncDIContainerError::CastFailed(type_name::< - Interface, - >( - )) - } - }, - )?, - ), - ) + } + })?, + )) } #[cfg(feature = "factory")] AsyncProvidable::Factory(factory_binding) => { match factory_binding.clone().cast::<Interface>() { Ok(factory) => Ok(SomeThreadsafePtr::ThreadsafeFactory(factory)), - Err(_err) => { + Err(first_err) => { use crate::interfaces::factory::IFactory; - let default_factory = - factory_binding - .cast::<dyn IFactory<(), Interface>>() - .map_err(|_| { - AsyncDIContainerError::CastFailed(type_name::< + if let CastError::NotArcCastable(_) = first_err { + return Err(AsyncDIContainerError::InterfaceNotAsync( + type_name::<Interface>(), + )); + } + + let default_factory = factory_binding + .cast::<dyn IFactory<(), Interface>>() + .map_err(|err| match err { + CastError::NotArcCastable(_) => { + AsyncDIContainerError::InterfaceNotAsync(type_name::< Interface, >( )) - })?; + } + CastError::CastFailed { from: _, to: _ } => { + AsyncDIContainerError::CastFailed { + interface: type_name::<Interface>(), + binding_kind: "factory", + } + } + })?; Ok(SomeThreadsafePtr::Transient(default_factory())) } |