axum/axum-macros/tests/from_request/pass/state_explicit.rs

46 lines
816 B
Rust
Raw Normal View History

use axum::{
extract::{FromRef, State},
routing::get,
2024-11-26 19:04:01 +01:00
Router,
};
2024-11-26 19:04:01 +01:00
use axum_macros::FromRequest;
fn main() {
let _: axum::Router = Router::new()
.route("/b", get(|_: Extractor| async {}))
.with_state(AppState::default());
}
#[derive(FromRequest)]
#[from_request(state(AppState))]
struct Extractor {
app_state: State<AppState>,
one: State<One>,
two: State<Two>,
other_extractor: String,
}
#[derive(Clone, Default)]
struct AppState {
one: One,
two: Two,
}
#[derive(Clone, Default)]
struct One {}
impl FromRef<AppState> for One {
fn from_ref(input: &AppState) -> Self {
input.one.clone()
}
}
#[derive(Clone, Default)]
struct Two {}
impl FromRef<AppState> for Two {
fn from_ref(input: &AppState) -> Self {
input.two.clone()
}
}