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
This commit is contained in:
David Pedersen
2022-10-10 18:40:14 +00:00
committed by GitHub
parent 1681ecf438
commit 9c0a89cd09
7 changed files with 108 additions and 2 deletions
+19
View File
@@ -0,0 +1,19 @@
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));
}