Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
- Loading branch information
1 parent
1681ecf
commit 9c0a89c
Showing
7 changed files
with
108 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use proc_macro2::{Ident, TokenStream}; | ||
use quote::quote_spanned; | ||
use syn::{spanned::Spanned, Field, ItemStruct}; | ||
|
||
pub(crate) fn expand(item: ItemStruct) -> TokenStream { | ||
item.fields | ||
.iter() | ||
.enumerate() | ||
.map(|(idx, field)| expand_field(&item.ident, idx, field)) | ||
.collect() | ||
} | ||
|
||
fn expand_field(state: &Ident, idx: usize, field: &Field) -> TokenStream { | ||
let field_ty = &field.ty; | ||
let span = field.ty.span(); | ||
|
||
let body = if let Some(field_ident) = &field.ident { | ||
quote_spanned! {span=> state.#field_ident.clone() } | ||
} else { | ||
let idx = syn::Index { | ||
index: idx as _, | ||
span: field.span(), | ||
}; | ||
quote_spanned! {span=> state.#idx.clone() } | ||
}; | ||
|
||
quote_spanned! {span=> | ||
impl ::axum::extract::FromRef<#state> for #field_ty { | ||
fn from_ref(state: &#state) -> Self { | ||
#body | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn ui() { | ||
crate::run_ui_tests("from_ref"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters