Trait runtime_injector::IntoFallible[][src]

pub trait IntoFallible<D, R, E, F> where
    D: Service,
    R: Service,
    E: Service + Error,
    F: ServiceFactory<D, Result = Result<R, E>>, 
{ #[must_use] fn fallible(self) -> FallibleServiceFactory<D, R, E, F>; }
Expand description

Defines a conversion into a fallible service factory. This trait is automatically implemented for all service factories that return a Result<T, E> with an error type that implements Error and Service.

Required methods

#[must_use]
fn fallible(self) -> FallibleServiceFactory<D, R, E, F>
[src]

Expand description

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"),
}
Loading content...

Implementors

impl<D, R, E, F> IntoFallible<D, R, E, F> for F where
    D: Service,
    R: Service,
    E: Service + Error,
    F: ServiceFactory<D, Result = Result<R, E>>, 
[src]

fn fallible(self) -> FallibleServiceFactory<D, R, E, F>[src]

Loading content...