|
148 | 148 | //! pool of database connections or clients to other services may need to
|
149 | 149 | //! be shared.
|
150 | 150 | //!
|
151 |
| -//! The three most common ways of doing that are: |
| 151 | +//! The four most common ways of doing that are: |
| 152 | +//! |
152 | 153 | //! - Using the [`State`] extractor
|
153 | 154 | //! - Using request extensions
|
154 | 155 | //! - Using closure captures
|
| 156 | +//! - Using task-local variables |
155 | 157 | //!
|
156 | 158 | //! ## Using the [`State`] extractor
|
157 | 159 | //!
|
|
182 | 184 | //! ```
|
183 | 185 | //!
|
184 | 186 | //! You should prefer using [`State`] if possible since it's more type safe. The downside is that
|
185 |
| -//! it's less dynamic than request extensions. |
| 187 | +//! it's less dynamic than task-local variables and request extensions. |
186 | 188 | //!
|
187 | 189 | //! See [`State`] for more details about accessing state.
|
188 | 190 | //!
|
189 | 191 | //! ## Using request extensions
|
190 | 192 | //!
|
191 |
| -//! Another way to extract state in handlers is using [`Extension`](crate::extract::Extension) as |
| 193 | +//! Another way to share state with handlers is using [`Extension`](crate::extract::Extension) as |
192 | 194 | //! layer and extractor:
|
193 | 195 | //!
|
194 | 196 | //! ```rust,no_run
|
|
273 | 275 | //! # let _: Router = app;
|
274 | 276 | //! ```
|
275 | 277 | //!
|
276 |
| -//! The downside to this approach is that it's a little more verbose than using |
277 |
| -//! [`State`] or extensions. |
| 278 | +//! The downside to this approach is that it's a the most verbose approach. |
278 | 279 | //!
|
279 |
| -//! ## Using [tokio's `task_local` macro](https://docs.rs/tokio/1/tokio/macro.task_local.html): |
| 280 | +//! ## Using task-local variables |
280 | 281 | //!
|
281 |
| -//! This allows to share state with `IntoResponse` implementations. |
| 282 | +//! This also allows to share state with `IntoResponse` implementations: |
282 | 283 | //!
|
283 | 284 | //! ```rust,no_run
|
284 | 285 | //! use axum::{
|
|
337 | 338 | //! .route_layer(middleware::from_fn(auth));
|
338 | 339 | //! ```
|
339 | 340 | //!
|
| 341 | +//! The main downside to this approach is that it only works when the async executor being used |
| 342 | +//! has the concept of task-local variables. The example above uses |
| 343 | +//! [tokio's `task_local` macro](https://docs.rs/tokio/1/tokio/macro.task_local.html). |
| 344 | +//! smol does not yet offer equivalent functionality at the time of writing (see |
| 345 | +//! [this GitHub issue](https://github.com/smol-rs/async-executor/issues/139)). |
| 346 | +//! |
340 | 347 | //! # Building integrations for axum
|
341 | 348 | //!
|
342 | 349 | //! Libraries authors that want to provide [`FromRequest`], [`FromRequestParts`], or
|
|
0 commit comments