mirror of
https://github.com/tokio-rs/axum.git
synced 2025-09-27 04:50:31 +00:00

* Make state type safe * fix examples * remove unnecessary `#[track_caller]`s * Router::into_service -> Router::with_state * fixup docs * macro docs * add missing docs * fix examples * format * changelog * Update trybuild tests * Make sure fallbacks are still inherited for opaque services (#1540) * Document nesting routers with different state * fix leftover conflicts
35 lines
714 B
Rust
35 lines
714 B
Rust
use axum_macros::FromRequestParts;
|
|
use axum::{
|
|
extract::{FromRef, State, Query},
|
|
Router,
|
|
routing::get,
|
|
};
|
|
use std::collections::HashMap;
|
|
|
|
fn main() {
|
|
let _: axum::routing::RouterService = Router::new()
|
|
.route("/b", get(|_: Extractor| async {}))
|
|
.with_state(AppState::default());
|
|
}
|
|
|
|
#[derive(FromRequestParts)]
|
|
#[from_request(state(AppState))]
|
|
struct Extractor {
|
|
inner_state: State<InnerState>,
|
|
other: Query<HashMap<String, String>>,
|
|
}
|
|
|
|
#[derive(Default, Clone)]
|
|
struct AppState {
|
|
inner: InnerState,
|
|
}
|
|
|
|
#[derive(Clone, Default)]
|
|
struct InnerState {}
|
|
|
|
impl FromRef<AppState> for InnerState {
|
|
fn from_ref(input: &AppState) -> Self {
|
|
input.inner.clone()
|
|
}
|
|
}
|