axum/axum-macros/tests/from_request/pass/state_via_parts.rs
David Pedersen 64960bb19c
Type safe state inheritance (#1532)
* 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
2022-11-18 11:02:58 +00:00

30 lines
772 B
Rust

use axum::{
extract::{FromRef, State},
routing::get,
Router,
};
use axum_macros::FromRequestParts;
fn main() {
let _: axum::routing::RouterService = Router::new()
.route("/a", get(|_: AppState, _: InnerState, _: String| async {}))
.route("/b", get(|_: AppState, _: String| async {}))
.route("/c", get(|_: InnerState, _: String| async {}))
.with_state(AppState::default());
}
#[derive(Clone, Default, FromRequestParts)]
#[from_request(via(State))]
struct AppState {
inner: InnerState,
}
#[derive(Clone, Default, FromRequestParts)]
#[from_request(via(State), state(AppState))]
struct InnerState {}
impl FromRef<AppState> for InnerState {
fn from_ref(input: &AppState) -> Self {
input.inner.clone()
}
}