Type safe state inheritance (#1532)

* Make state type safe

* fix examples

* remove unnecessary `#[track_caller]`s

* Router::into_service -> Router::with_state

* fixup docs

* macro docs

* add missing docs

* fix examples

* format

* changelog

* Update trybuild tests

* Make sure fallbacks are still inherited for opaque services (#1540)

* Document nesting routers with different state

* fix leftover conflicts
This commit is contained in:
David Pedersen
2022-11-18 11:02:58 +00:00
committed by GitHub
parent ba8e9c1b21
commit 64960bb19c
62 changed files with 675 additions and 736 deletions
@@ -3,7 +3,6 @@ use axum::{
extract::{FromRequest, Json},
response::Response,
};
use axum_macros::FromRequest;
use serde::Deserialize;
#[derive(Deserialize, FromRequest)]
@@ -2,7 +2,6 @@ use axum::{
extract::{FromRequestParts, Extension},
response::Response,
};
use axum_macros::FromRequestParts;
#[derive(Clone, FromRequestParts)]
#[from_request(via(Extension))]
@@ -4,7 +4,6 @@ use axum::{
response::Response,
headers::{self, UserAgent},
};
use axum_macros::FromRequest;
#[derive(FromRequest)]
struct Extractor {
@@ -3,7 +3,6 @@ use axum::{
headers::{self, UserAgent},
response::Response,
};
use axum_macros::FromRequestParts;
#[derive(FromRequestParts)]
struct Extractor {
@@ -7,7 +7,6 @@ use axum::{
},
headers::{self, UserAgent},
};
use axum_macros::FromRequest;
#[derive(FromRequest)]
struct Extractor {
@@ -6,7 +6,6 @@ use axum::{
},
headers::{self, UserAgent},
};
use axum_macros::FromRequestParts;
#[derive(FromRequestParts)]
struct Extractor {
@@ -6,7 +6,6 @@ use axum::{
routing::get,
Extension, Router,
};
use axum_macros::FromRequest;
fn main() {
let _: Router = Router::new().route("/", get(handler).post(handler_result));
@@ -6,7 +6,6 @@ use axum::{
routing::get,
Extension, Router,
};
use axum_macros::FromRequestParts;
fn main() {
let _: Router = Router::new().route("/", get(handler).post(handler_result));
@@ -6,9 +6,10 @@ use axum::{
use axum_macros::FromRequest;
fn main() {
let _: Router<AppState> = Router::with_state(AppState::default())
let _: axum::routing::RouterService = Router::new()
.route("/a", get(|_: AppState| async {}))
.route("/b", get(|_: InnerState| async {}));
.route("/b", get(|_: InnerState| async {}))
.with_state(AppState::default());
}
#[derive(Clone, FromRequest)]
@@ -6,10 +6,11 @@ use axum::{
use axum_macros::FromRequestParts;
fn main() {
let _: Router<AppState> = Router::with_state(AppState::default())
let _: axum::routing::RouterService = Router::new()
.route("/a", get(|_: AppState| async {}))
.route("/b", get(|_: InnerState| async {}))
.route("/c", get(|_: AppState, _: InnerState| async {}));
.route("/c", get(|_: AppState, _: InnerState| async {}))
.with_state(AppState::default());
}
#[derive(Clone, FromRequestParts)]
@@ -6,8 +6,9 @@ use axum::{
};
fn main() {
let _: Router<AppState> = Router::with_state(AppState::default())
.route("/b", get(|_: Extractor| async {}));
let _: axum::routing::RouterService = Router::new()
.route("/b", get(|_: Extractor| async {}))
.with_state(AppState::default());
}
#[derive(FromRequest)]
@@ -7,8 +7,9 @@ use axum::{
use std::collections::HashMap;
fn main() {
let _: Router<AppState> = Router::with_state(AppState::default())
.route("/b", get(|_: Extractor| async {}));
let _: axum::routing::RouterService = Router::new()
.route("/b", get(|_: Extractor| async {}))
.with_state(AppState::default());
}
#[derive(FromRequestParts)]
@@ -6,8 +6,9 @@ use axum::{
use axum_macros::FromRequest;
fn main() {
let _: Router<AppState> = Router::with_state(AppState::default())
.route("/", get(|_: Extractor| async {}));
let _: axum::routing::RouterService = Router::new()
.route("/", get(|_: Extractor| async {}))
.with_state(AppState::default());
}
#[derive(FromRequest)]
@@ -6,8 +6,9 @@ use axum::{
use axum_macros::FromRequest;
fn main() {
let _: Router<AppState> = Router::with_state(AppState::default())
.route("/", get(|_: Extractor| async {}));
let _: axum::routing::RouterService = Router::new()
.route("/", get(|_: Extractor| async {}))
.with_state(AppState::default());
}
#[derive(FromRequest)]
@@ -6,9 +6,10 @@ use axum::{
use axum_macros::FromRequest;
fn main() {
let _: Router<AppState> = Router::with_state(AppState::default())
let _: axum::routing::RouterService = Router::new()
.route("/b", get(|_: (), _: AppState| async {}))
.route("/c", get(|_: (), _: InnerState| async {}));
.route("/c", get(|_: (), _: InnerState| async {}))
.with_state(AppState::default());
}
#[derive(Clone, Default, FromRequest)]
@@ -6,8 +6,9 @@ use axum::{
use axum_macros::FromRequest;
fn main() {
let _: Router<AppState> = Router::with_state(AppState::default())
.route("/b", get(|_: AppState| async {}));
let _: axum::routing::RouterService = Router::new()
.route("/b", get(|_: AppState| async {}))
.with_state(AppState::default());
}
// if we're extract "via" `State<AppState>` and not specifying state
@@ -6,10 +6,11 @@ use axum::{
use axum_macros::FromRequestParts;
fn main() {
let _: Router<AppState> = Router::with_state(AppState::default())
let _: axum::routing::RouterService = Router::new()
.route("/a", get(|_: AppState, _: InnerState, _: String| async {}))
.route("/b", get(|_: AppState, _: String| async {}))
.route("/c", get(|_: InnerState, _: String| async {}));
.route("/c", get(|_: InnerState, _: String| async {}))
.with_state(AppState::default());
}
#[derive(Clone, Default, FromRequestParts)]
@@ -8,8 +8,9 @@ use axum::{
use axum_macros::FromRequest;
fn main() {
let _: Router<AppState> =
Router::with_state(AppState::default()).route("/a", get(|_: Extractor| async {}));
let _: axum::routing::RouterService = Router::new()
.route("/a", get(|_: Extractor| async {}))
.with_state(AppState::default());
}
#[derive(Clone, Default, FromRequest)]