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]
impl<R> Factory<R> where
R: Request,
[src]pub fn get(&self) -> Result<R, InjectError>
[src]
pub fn get(&self) -> Result<R, InjectError>
[src]Performs the factory’s inner request.
#[must_use]pub fn request_info(&self) -> &RequestInfo
[src]
#[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]
#[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
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> BorrowMut<T> for T where
T: ?Sized,
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]pub fn borrow_mut(&mut self) -> &mut T
[src]
pub fn borrow_mut(&mut self) -> &mut T
[src]Mutably borrows from an owned value. Read more
impl<T> Instrument for T
[src]
impl<T> Instrument for T
[src]fn instrument(self, span: Span) -> Instrumented<Self>
[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]
fn in_current_span(self) -> Instrumented<Self>
[src]impl<T> Instrument for T
[src]
impl<T> Instrument for T
[src]fn instrument(self, span: Span) -> Instrumented<Self>
[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]
fn in_current_span(self) -> Instrumented<Self>
[src]impl<T> Interface for T where
T: Service,
[src]
impl<T> Interface for T where
T: Service,
[src]impl<T> RequestParameter for T where
T: Service + Clone + AsAny,
[src]
impl<T> RequestParameter for T where
T: Service + Clone + AsAny,
[src]pub fn clone_dyn(&self) -> Box<dyn RequestParameter + 'static, Global>
[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
impl<T> Same<T> for T
type Output = T
type Output = T
Should always be Self
impl<T> ToOwned for T where
T: Clone,
[src]
impl<T> ToOwned for T where
T: Clone,
[src]type Owned = T
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
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]
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<V, T> VZip<V> for T where
V: MultiLane<T>,
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]
T: Service,
impl<T> Service for T where
T: Any + Send + Sync + ?Sized,
[src]
T: Any + Send + Sync + ?Sized,