Skip to content

Commit

Permalink
Improve State and Router docs.
Browse files Browse the repository at this point in the history
Per my comment in tokio-rs#1313, this improves discoverability of how `State`
works with nested/merged `Router`s.

I made a few other minor documentation fixes while I was reading:

* Corrected the docs for `Router::nest` to say that it nests another
  `Router` rather than a `Service`. There is a separate function
  (`Router::nest_service`) for the latter.
* Changed one of the headings in nest.md to use more idiomatic English.
* Changed awkward phrasing under the "Sharing state with handlers"
  heading of the root documentation page.
* Removed a trailing period from one of three list items for
  consistency.
  • Loading branch information
jimmycuadra committed Nov 19, 2022
1 parent 2e3000f commit 0649f62
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
4 changes: 2 additions & 2 deletions axum/src/docs/routing/nest.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Nest a [`Service`] at some path.
Nest a [`Router`] at some path.

This allows you to break your application into smaller pieces and compose
them together.
Expand Down Expand Up @@ -64,7 +64,7 @@ let app = Router::new().nest("/:version/api", users_api);
# };
```

# Differences to wildcard routes
# Differences from wildcard routes

Nested routes are similar to wildcard routes. The difference is that
wildcard routes still see the whole URI whereas nested routes will have
Expand Down
11 changes: 10 additions & 1 deletion axum/src/extract/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{

/// Extractor for state.
///
/// See ["Accessing state in middleware"][state-from-middleware] for how to
/// See ["Accessing state in middleware"][state-from-middleware] for how to
/// access state in middleware.
///
/// [state-from-middleware]: crate::middleware#accessing-state-in-middleware
Expand Down Expand Up @@ -46,6 +46,15 @@ use std::{
/// # let _: axum::routing::RouterService = app;
/// ```
///
/// [`Router`][router]s that are combined with [`Router::nest`][router-nest]
/// or [`Router::merge`][router-merge] generally require the same type of state.
/// See [`Router::nest`][router-nest] for how to combine routers with different
/// types of state.
///
/// [router]: crate::Router
/// [router-nest]: crate::Router::nest
/// [router-merge]: crate::Router::merge
///
/// # With `MethodRouter`
///
/// ```
Expand Down
7 changes: 4 additions & 3 deletions axum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,12 @@
//!
//! # Sharing state with handlers
//!
//! It is common to share some state between handlers for example to share a
//! pool of database connections or clients to other services.
//! It is common to share some state between handlers. For example, a
//! pool of database connections or clients to other services may need to
//! be shared.
//!
//! The three most common ways of doing that are:
//! - Using the [`State`] extractor.
//! - Using the [`State`] extractor
//! - Using request extensions
//! - Using closure captures
//!
Expand Down

0 comments on commit 0649f62

Please sign in to comment.