Struct runtime_injector_actix::Factory[][src]

pub struct Factory<R> where
    R: Request
{ /* fields omitted */ }
Expand description

Lazy request factory allowing requests to be made outside of service creation.

This lets a service make requests to the injector outside of the service’s constructor. For instance, the service can make requests for one of its dependencies at any time, and as many times as it wants, outside of the service’s constructor.

Factory can be cloned, making it easy to specialize each request made by the factory as needed.

Example

use runtime_injector::{
    Factory, Injector, IntoSingleton, IntoTransient, Svc,
};

#[derive(Default)]
struct Foo;
struct Bar(Factory<Box<Foo>>);

let mut builder = Injector::builder();
builder.provide(Foo::default.transient());
builder.provide(Bar.singleton());

let injector = builder.build();
let bar: Svc<Bar> = injector.get().unwrap();
let _foo1 = bar.0.get().unwrap();
let _foo2 = bar.0.get().unwrap();
// ...

Implementations

impl<R> Factory<R> where
    R: Request
[src]

pub fn get(&self) -> Result<R, InjectError>[src]

Performs the factory’s inner request.

#[must_use]
pub fn request_info(&self) -> &RequestInfo
[src]

Gets this factory’s inner RequestInfo. This request info is used by all requests the factory makes.

#[must_use]
pub fn request_info_mut(&mut self) -> &mut RequestInfo
[src]

Mutably gets this factory’s inner RequestInfo. This request info is used by all requests the factory makes.

Modifying this request info affects future requests the factory makes, meaning additional arguments can be added to requests prior to them being executed. Since the factory can be cloned, requests can be specialized by first cloning the factory, then modifying the RequestInfo on the clone and using it to make the request instead.

Example

use runtime_injector::{
    Arg, Factory, InjectResult, Injector, IntoSingleton, IntoTransient,
    Svc, WithArg,
};

struct Foo(Arg<i32>);

struct Bar(Factory<Box<Foo>>);
impl Bar {
    fn get_foo(&self, arg: i32) -> InjectResult<Box<Foo>> {
        let mut factory = self.0.clone();
        factory.request_info_mut().with_arg::<Foo, i32>(arg);
        factory.get()
    }
}

let mut builder = Injector::builder();
builder.provide(Foo.transient());
builder.provide(Bar.singleton());

let injector = builder.build();
let bar: Svc<Bar> = injector.get().unwrap();
let foo1 = bar.get_foo(1).unwrap();
let foo2 = bar.get_foo(2).unwrap();

assert_eq!(1, *foo1.0);
assert_eq!(2, *foo2.0);

Trait Implementations

impl<R> Clone for Factory<R> where
    R: Request
[src]

pub fn clone(&self) -> Factory<R>[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl<R> Request for Factory<R> where
    R: Request
[src]

Lazy request factory allowing requests to be made outside of service creation.

pub fn request(
    injector: &Injector,
    info: &RequestInfo
) -> Result<Factory<R>, InjectError>
[src]

Performs the request to the injector.

Auto Trait Implementations

impl<R> !RefUnwindSafe for Factory<R>

impl<R> Send for Factory<R>

impl<R> Sync for Factory<R>

impl<R> Unpin for Factory<R>

impl<R> !UnwindSafe for Factory<R>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> AsAny for T where
    T: Any
[src]

pub fn as_any(&self) -> &(dyn Any + 'static)[src]

Converts self into a trait object.

pub fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)[src]

Converts self into a mutable trait object.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T> Instrument for T[src]

fn instrument(self, span: Span) -> Instrumented<Self>[src]

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

fn in_current_span(self) -> Instrumented<Self>[src]

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

impl<T> Instrument for T[src]

fn instrument(self, span: Span) -> Instrumented<Self>[src]

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

fn in_current_span(self) -> Instrumented<Self>[src]

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

impl<T> Interface for T where
    T: Service
[src]

pub fn downcast(
    service: Arc<dyn Any + 'static + Sync + Send>
) -> Result<Arc<T>, InjectError>
[src]

Downcasts a dynamic service pointer into a service pointer of this interface type. Read more

pub fn downcast_owned(
    service: Box<dyn Any + 'static + Sync + Send, Global>
) -> Result<Box<T, Global>, InjectError>
[src]

Downcasts an owned dynamic service pointer into an owned service pointer of this interface type. Read more

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> RequestParameter for T where
    T: Service + Clone + AsAny
[src]

pub fn clone_dyn(&self) -> Box<dyn RequestParameter + 'static, Global>[src]

Clones this parameter into a boxed trait object.

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

Creates owned data from borrowed data, usually by cloning. Read more

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>, 

pub fn vzip(self) -> V

impl<T> InterfaceFor<T> for T where
    T: Service
[src]

impl<T> Service for T where
    T: Any + Send + Sync + ?Sized
[src]