mirror of
https://github.com/tokio-rs/axum.git
synced 2024-11-22 23:30:29 +01:00
45 lines
805 B
Rust
45 lines
805 B
Rust
|
use axum_macros::FromRequest;
|
||
|
use axum::{
|
||
|
extract::{FromRef, State},
|
||
|
Router,
|
||
|
routing::get,
|
||
|
};
|
||
|
|
||
|
fn main() {
|
||
|
let _: Router<AppState> = Router::with_state(AppState::default())
|
||
|
.route("/b", get(|_: Extractor| async {}));
|
||
|
}
|
||
|
|
||
|
#[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()
|
||
|
}
|
||
|
}
|