Struct shaku_rocket::Inject[][src]

pub struct Inject<'r, M: ModuleInterface + HasComponent<I> + ?Sized, I: Interface + ?Sized>(_, _);
Expand description

Used to retrieve a reference to a component from a shaku Module. The module should be stored in Rocket’s state, in a Box (It could be Box<dyn MyModule> if the module implementation changes at runtime). Use this Inject struct as a request guard.

Example

#[macro_use] extern crate rocket;

use shaku::{module, Component, Interface};
use shaku_rocket::Inject;

trait HelloWorld: Interface {
    fn greet(&self) -> String;
}

#[derive(Component)]
#[shaku(interface = HelloWorld)]
struct HelloWorldImpl;

impl HelloWorld for HelloWorldImpl {
    fn greet(&self) -> String {
        "Hello, world!".to_owned()
    }
}

module! {
    HelloModule {
        components = [HelloWorldImpl],
        providers = []
    }
}

#[get("/")]
fn hello(hello_world: Inject<HelloModule, dyn HelloWorld>) -> String {
    hello_world.greet()
}

#[rocket::launch]
fn rocket() -> _ {
    let module = HelloModule::builder().build();

    rocket::build()
        .manage(Box::new(module))
        .mount("/", routes![hello])
}

Trait Implementations

The resulting type after dereferencing.

Dereferences the value.

The associated error to be returned if derivation fails.

Derives an instance of Self from the incoming request metadata. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

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

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

Performs the conversion.

Converts self into a collection.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.