axum/axum-macros/tests/from_request/pass/state_explicit.rs
David Pedersen 0b26411f39
Change Router::with_state and impl Service for Router<()> (#1552)
* Implement `Service` for `Router<(), B>`

* wip

* wip

* fix some tests

* fix examples

* fix doc tests

* clean up docs

* changelog

* fix

* also call `with_state` when converting `MethodRouter` into a `MakeService`

* suggestions from review
2022-11-24 14:43:10 +00:00

45 lines
816 B
Rust

use axum_macros::FromRequest;
use axum::{
extract::{FromRef, State},
Router,
routing::get,
};
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()
}
}