Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve docs regarding state and extensions #2991

Merged
merged 4 commits into from
Oct 21, 2024

Conversation

Lachstec
Copy link
Contributor

@Lachstec Lachstec commented Oct 16, 2024

Motivation

Issue #2917 mentions that it should be easier to discover the documentation for Extension from the docs of State and vice versa. Indeed, Extension is very easy to miss as it is only mentioned after a couple of examples.

Solution

This PR updates the docs by

  • moving the section mentioning extensions up in Router::with_state
  • Adding a short mention of Extension to extract::State

Closes #2917

@lolbinarycat
Copy link

lolbinarycat commented Oct 16, 2024

"state derived from a request" seems like a fairly narrow usecase. Extension is also useful when converting a previously stateless system into a stateful one, since changing the type of the State parameter also changes the type of the router, which can cause issues.

Copy link
Collaborator

@mladedav mladedav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"state derived from a request" seems like a fairly narrow usecase. Extension is also useful when converting a previously stateless system into a stateful one, since changing the type of the State parameter also changes the type of the router, which can cause issues.

I agree with how the docs are written now. Extension has been historically used for passing around application state, but users should use State for that now. It's type safe and more maintainable.

I'd recommend anyone who needs to make their application stateful to use State instead of Extension even though they will have to make more changes if they're passing around Routers. I can't think of any scenarios other than the one in docs where I'd recommend an Extension.

@lolbinarycat
Copy link

well, i genuinely could not get State to function properly when i tried. does every handler need to accept a State parameter for it to work?

@jplatte
Copy link
Member

jplatte commented Oct 21, 2024

@lolbinarycat No, that's not required. Please open a new discussion under Q&A to get help with that.

@mladedav mladedav merged commit 280d16a into tokio-rs:main Oct 21, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

make it easier to get from State to Extension
4 participants