axum/axum-macros/tests/from_ref/pass/basic.rs
David Pedersen 9c0a89cd09
Add #[derive(FromRef)] (#1430)
* add `#[derive(FromRef)]`

* tests

* don't support skipping fields

probably wouldn't work at all since the whole state likely needs `Clone`

* UI tests

* changelog

* changelog link

* revert hello-world example, used for testing

* Re-export `#[derive(FromRef)]`

* Don't need to return `Result`

* use `collect` instead of quoting the iterator

* Mention it in axum's changelog
2022-10-10 18:40:14 +00:00

19 lines
468 B
Rust

use axum_macros::FromRef;
use axum::{Router, routing::get, extract::State};
// This will implement `FromRef` for each field in the struct.
#[derive(Clone, FromRef)]
struct AppState {
auth_token: String,
}
// So those types can be extracted via `State`
async fn handler(_: State<String>) {}
fn main() {
let state = AppState {
auth_token: Default::default(),
};
let _: Router<AppState> = Router::with_state(state).route("/", get(handler));
}