API improvements

- Added public `Error`, `Result`, `InitFuture`, and `ShutdownFuture`
  aliases.
- Added `TypeMapState` as the default state instead of `Arc<TypeMap>`.
- Added `App::route`, `App::mount`, and `App::store`.
- Changed `App::init()` to return `InitializedApp<S>`.
- Added `InitializedApp::router()`, `state()`, `into_parts()`, and
  `shutdown()`.
- Made shutdown hooks fallible: `Result<()>`.
- Made `AdHocPlugin::on_shutdown` accept capturing closures.
- Added `Default` for `App` and `AdHocPlugin`.

Improved AppState macro:
- Uses `axum_app_wrapper::Error`.
- Supports generic state structs.
- Fixed the stale `Arc<AppState>` doc snippet.
This commit is contained in:
2026-05-27 01:04:50 -04:00
parent 958060e538
commit 1d6708e23b
4 changed files with 254 additions and 86 deletions

View File

@@ -22,10 +22,10 @@ use syn::{Data, DeriveInput, Fields, parse_macro_input};
/// }
/// // To wrap the whole state in `Arc`, implement `TryFrom<TypeMap>` for `Arc<AppState>`:
/// impl TryFrom<TypeMap> for Arc<AppState> {
/// type Error = anyhow::Error;
/// type Error = axum_app_wrapper::Error;
///
/// fn try_from(map: TypeMap) -> Result<Self, Self::Error> {
/// Ok(Self(Arc::new(AppState::try_from(map)?)))
/// Ok(Arc::new(AppState::try_from(map)?))
/// }
/// }
/// ```
@@ -57,10 +57,11 @@ pub fn derive_app_state(input: TokenStream) -> TokenStream {
};
let field_names = fields.iter().map(|f| &f.ident);
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
quote! {
impl ::std::convert::TryFrom<::axum_app_wrapper::TypeMap> for #name {
type Error = ::anyhow::Error;
impl #impl_generics ::std::convert::TryFrom<::axum_app_wrapper::TypeMap> for #name #ty_generics #where_clause {
type Error = ::axum_app_wrapper::Error;
fn try_from(mut map: ::axum_app_wrapper::TypeMap) -> ::std::result::Result<Self, Self::Error> {
Ok(#name {