Struct runtime_injector::RequestInfo[][src]

pub struct RequestInfo { /* fields omitted */ }
Expand description

Information about an active request.

Implementations

impl RequestInfo[src]

#[must_use]
pub fn new() -> Self
[src]

Creates a new, empty instance of RequestInfo.

#[must_use]
pub fn with_request(&self, service: ServiceInfo) -> Self
[src]

Creates a new child instance of RequestInfo with the given service appended to the end of the request path.

#[must_use]
pub fn service_path(&self) -> &[ServiceInfo]
[src]

Gets the current request path. This can be used to configure a service based on what it’s being injected into.

Example

use runtime_injector::{
    Injector, IntoTransient, RequestInfo, ServiceInfo, Svc,
};

struct Foo(pub Svc<Baz>);
struct Bar(pub Svc<Baz>);
struct Baz(pub i32);

impl Baz {
    pub fn new(request_info: RequestInfo) -> Self {
        let service_path = request_info.service_path();
        let value = match service_path.get(0) {
            Some(root) if root == &ServiceInfo::of::<Foo>() => 1,
            Some(root) if root == &ServiceInfo::of::<Bar>() => 2,
            _ => 0,
        };

        Baz(value)
    }
}

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

let injector = builder.build();
let foo: Svc<Foo> = injector.get().unwrap();
let bar: Svc<Bar> = injector.get().unwrap();
let baz: Svc<Baz> = injector.get().unwrap();
assert_eq!(1, foo.0.0);
assert_eq!(2, bar.0.0);
assert_eq!(0, baz.0);

pub fn insert_parameter(
    &mut self,
    key: &str,
    value: impl RequestParameter
) -> Option<Box<dyn RequestParameter>>
[src]

Sets the value of a request parameter for the request. If a parameter has already been set to a value, then that value is returned.

pub fn remove_parameter(
    &mut self,
    key: &str
) -> Option<Box<dyn RequestParameter>>
[src]

Removes and returns the value of a parameter if it has been set.

#[must_use]
pub fn get_parameter(&self, key: &str) -> Option<&dyn RequestParameter>
[src]

Gets the value of a parameter if it has been set.

#[must_use]
pub fn get_parameter_mut(
    &mut self,
    key: &str
) -> Option<&mut dyn RequestParameter>
[src]

Mutably gets the value of a parameter if it has been set.

Trait Implementations

impl Clone for RequestInfo[src]

fn clone(&self) -> RequestInfo[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 Debug for RequestInfo[src]

fn fmt(&self, f: &mut Formatter<'_>) -> Result[src]

Formats the value using the given formatter. Read more

impl Default for RequestInfo[src]

fn default() -> Self[src]

Returns the “default value” for a type. Read more

impl Request for RequestInfo[src]

Requests the information about the current request.

fn request(_injector: &Injector, info: &RequestInfo) -> InjectResult<Self>[src]

Performs the request to the injector.

impl WithArg for RequestInfo[src]

fn with_arg<S: Service, T: Service + AsAny + Clone>(
    &mut self,
    value: T
) -> Option<Box<dyn RequestParameter>>
[src]

Adds an argument for a service. See the docs for Arg<T>.

Auto Trait Implementations

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> 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, U> Into<U> for T where
    U: From<T>, 
[src]

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

Performs the conversion.

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.