mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-29 00:00:18 +02:00
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:
@@ -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));
|
||||
}
|
||||
Reference in New Issue
Block a user