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

Allow Routers to inherit state, take 2 #1368

Merged
merged 7 commits into from Sep 25, 2022
Merged

Allow Routers to inherit state, take 2 #1368

merged 7 commits into from Sep 25, 2022

Conversation

jplatte
Copy link
Member

@jplatte jplatte commented Sep 11, 2022

Motivation

Closes #1326.

Solution

Make Router<S, _> no longer a type that necessarily holds a state instance of type S. Instead, we track (at runtime, to avoid making the generics too complex) whether a given Router instance wants to inherit its state.

Status

I implemented router merging which was much easier than splitting nest into nest and nest_service. The fallback handler (currently stubbed out) will also need some special handling, but I don't think it should be a showstopper.

@jplatte
Copy link
Member Author

jplatte commented Sep 16, 2022

@davidpdrsn Have you had a look? I might have some time today or in the coming days to come back to this.

@jplatte jplatte force-pushed the state-inheritance-2 branch 3 times, most recently from d855343 to 52e9c56 Compare September 17, 2022 00:46
Copy link
Member

@davidpdrsn davidpdrsn left a comment

Choose a reason for hiding this comment

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

I like the direction of this! I think its a good balance between usability and type safety.

axum/src/routing/mod.rs Outdated Show resolved Hide resolved
axum/src/routing/mod.rs Show resolved Hide resolved
@davidpdrsn
Copy link
Member

I implemented router merging which was much easier than splitting nest into nest and nest_service.

I'm fine with merging this without support for nest. Then we can do that in a follow up PR.

@jplatte jplatte marked this pull request as ready for review September 22, 2022 14:41
@jplatte
Copy link
Member Author

jplatte commented Sep 22, 2022

So I basically re-did everything except boxed.rs with the latest force-push. Things should now be much cleaner.

@jplatte
Copy link
Member Author

jplatte commented Sep 22, 2022

I also think we can get rid of passing state via Extension with the boxed handler approach, but that is a big refactoring on its own that I don't want to mix into this PR.

@jplatte
Copy link
Member Author

jplatte commented Sep 22, 2022

Rebased to fix the unrelated trybuild failure.

@jplatte
Copy link
Member Author

jplatte commented Sep 23, 2022

Added nest, it was sooo easy. Hopefully the implementation is correct 😄

@jplatte jplatte force-pushed the state-inheritance-2 branch 3 times, most recently from 7cf1d13 to 21558eb Compare September 23, 2022 18:49
@jplatte jplatte changed the title Allow Routers to inherit state and fallback route, take 2 Allow Routers to inherit state, take 2 Sep 25, 2022
Copy link
Member

@davidpdrsn davidpdrsn left a comment

Choose a reason for hiding this comment

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

I think it looks good! I'll write some docs in a follow-up PR.

Wanna update the changelog? Then we can merge this 🚀

EDIT: We already have #1313 so wont file a separate issue.

@davidpdrsn davidpdrsn enabled auto-merge (squash) September 25, 2022 11:39
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.

Nesting/merging routers that use state could be more ergonomic
2 participants