Trait runtime_injector_actix::IntoFallible [−][src]
pub trait IntoFallible<D, R, E, F> where
R: Service,
E: Service + Error,
F: ServiceFactory<D, Result = Result<R, E>>,
D: Service, { #[must_use] fn fallible(self) -> FallibleServiceFactory<D, R, E, F>; }
Defines a conversion into a fallible service factory. This trait is
automatically implemented for all service factories that return a
Result<T, E>
with a type that implements Error
+ Service
.
Required methods
#[must_use]fn fallible(self) -> FallibleServiceFactory<D, R, E, F>
[src]
#[must_use]
fn fallible(self) -> FallibleServiceFactory<D, R, E, F>Marks a service factory as being able to fail. On failure, an injection
error is returned during activation. On success, the service is
injected unwrapped from the result. In other words, a Result<T, E>
can be requested as a Svc<T>
, however if the
constructor fails, an injection error is returned from the request.
Example
use runtime_injector::{ InjectError, InjectResult, Injector, IntoFallible, IntoTransient, Svc, }; use std::{ error::Error, fmt::{Display, Formatter}, }; #[derive(Debug)] struct FooError; impl Error for FooError {} impl Display for FooError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "An error occurred while creating a Foo") } } struct Foo(Svc<i32>); fn make_foo(a: Svc<i32>) -> Result<Foo, FooError> { Err(FooError) } let mut builder = Injector::builder(); builder.provide(make_foo.fallible().transient()); builder.provide((|| 0).transient()); let injector = builder.build(); let foo_result: InjectResult<Svc<Foo>> = injector.get(); match foo_result { Err(InjectError::ActivationFailed { .. }) => {} Err(error) => Err(error).unwrap(), _ => unreachable!("activation should have failed"), }
Implementors
impl<D, R, E, F> IntoFallible<D, R, E, F> for F where
R: Service,
E: Service + Error,
F: ServiceFactory<D, Result = Result<R, E>>,
D: Service,
[src]
impl<D, R, E, F> IntoFallible<D, R, E, F> for F where
R: Service,
E: Service + Error,
F: ServiceFactory<D, Result = Result<R, E>>,
D: Service,
[src]