From 8bc326cc3deefda4604e9cc780c24bcd5613723e Mon Sep 17 00:00:00 2001 From: Leon Lux Date: Mon, 21 Oct 2024 21:47:20 +0200 Subject: [PATCH] Improve docs regarding state and extensions (#2991) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: David Mládek --- axum/src/docs/routing/with_state.md | 10 ++-------- axum/src/extract/state.rs | 4 ++++ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/axum/src/docs/routing/with_state.md b/axum/src/docs/routing/with_state.md index 973a87e0..197741cf 100644 --- a/axum/src/docs/routing/with_state.md +++ b/axum/src/docs/routing/with_state.md @@ -1,4 +1,5 @@ -Provide the state for the router. +Provide the state for the router. State passed to this method is global and will be used +for all requests this router receives. That means it is not suitable for holding state derived from a request, such as authorization data extracted in a middleware. Use [`Extension`] instead for such data. ```rust use axum::{Router, routing::get, extract::State}; @@ -94,13 +95,6 @@ axum::serve(listener, routes).await.unwrap(); # }; ``` -# State is global within the router - -The state passed to this method will be used for all requests this router -receives. That means it is not suitable for holding state derived from a -request, such as authorization data extracted in a middleware. Use [`Extension`] -instead for such data. - # What `S` in `Router` means `Router` means a router that is _missing_ a state of type `S` to be able to diff --git a/axum/src/extract/state.rs b/axum/src/extract/state.rs index fb401c00..82e1e6e9 100644 --- a/axum/src/extract/state.rs +++ b/axum/src/extract/state.rs @@ -11,7 +11,11 @@ use std::{ /// See ["Accessing state in middleware"][state-from-middleware] for how to /// access state in middleware. /// +/// State is global and used in every request a router with state receives. +/// For accessing data derived from requests, such as authorization data, see [`Extension`]. +/// /// [state-from-middleware]: crate::middleware#accessing-state-in-middleware +/// [`Extension`]: crate::Extension /// /// # With `Router` ///