pub struct Inject<M: ModuleInterface + HasComponent<I> + ?Sized, I: Interface + ?Sized>(/* private fields */);Expand description
Used to retrieve a reference to a component from a shaku Module.
The module should be stored in Axum state, wrapped in an Arc (Arc<MyModule>).
This Arc<MyModule> must implement FromRef<S> where S is the Axum state type.
Use this struct as an extractor.
§Example
use axum::{routing::get, Router};
use axum::extract::{Extension, FromRef};
use shaku::{module, Component, Interface};
use shaku_axum::Inject;
use std::net::SocketAddr;
use std::sync::Arc;
use tokio::net::TcpListener;
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 = []
}
}
#[derive(Clone)]
struct AppState {
module: Arc<HelloModule>,
}
impl FromRef<AppState> for Arc<HelloModule> {
fn from_ref(app_state: &AppState) -> Arc<HelloModule> {
app_state.module.clone()
}
}
async fn hello(hello_world: Inject<HelloModule, dyn HelloWorld>) -> String {
hello_world.greet()
}
#[tokio::main]
async fn main() {
let module = Arc::new(HelloModule::builder().build());
let state = AppState { module };
let app = Router::new()
.route("/", get(hello))
.with_state(state);
let listener = TcpListener::bind("127.0.0.1:8080").await.unwrap();
axum::serve(listener, app.into_make_service())
.await
.unwrap();
}Trait Implementations§
Source§impl<M: ModuleInterface + HasComponent<I> + ?Sized, I: Interface + ?Sized> Deref for Inject<M, I>
impl<M: ModuleInterface + HasComponent<I> + ?Sized, I: Interface + ?Sized> Deref for Inject<M, I>
Source§impl<S, M, I> FromRequestParts<S> for Inject<M, I>where
S: Send + Sync,
M: ModuleInterface + HasComponent<I> + ?Sized,
I: Interface + ?Sized,
Arc<M>: FromRef<S>,
impl<S, M, I> FromRequestParts<S> for Inject<M, I>where
S: Send + Sync,
M: ModuleInterface + HasComponent<I> + ?Sized,
I: Interface + ?Sized,
Arc<M>: FromRef<S>,
Auto Trait Implementations§
impl<M, I> Freeze for Inject<M, I>
impl<M, I> RefUnwindSafe for Inject<M, I>
impl<M, I> Send for Inject<M, I>
impl<M, I> Sync for Inject<M, I>
impl<M, I> Unpin for Inject<M, I>
impl<M, I> UnwindSafe for Inject<M, I>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<S, T> FromRequest<S, ViaParts> for T
impl<S, T> FromRequest<S, ViaParts> for T
Source§type Rejection = <T as FromRequestParts<S>>::Rejection
type Rejection = <T as FromRequestParts<S>>::Rejection
If the extractor fails it’ll use this “rejection” type. A rejection is
a kind of error that can be converted into a response.
Source§fn from_request(
req: Request<Body>,
state: &S,
) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>
fn from_request( req: Request<Body>, state: &S, ) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>
Perform the extraction.