-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Change Router::with_state
and impl Service for Router<()>
#1552
Conversation
So I've been wondering if this plus adding Having to call |
I don't have a strong opinion on that. I kind of like how |
It might be clearer to use the name Router::new()
.merge(router_with_other_state.with_state(other_state))
.into_stateful_service(outer_route_state) |
f1b935b
to
38c5961
Compare
Router::provide_state
and MethodRouter::provide_state
Router::with_state
and impl Service for Router<()>
@jplatte this is ready for review :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a really nice improvement! Great work! I found a couple small things you might want to change in the docs before merging.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apart from possible doc improvements, this seems good. I really like this solution to the state inheritance problem!
After reading the release notes and the discussion above, I am still not quite sure how to async fn serve(router: Router<MyState>) {
let socket = SocketAddr::new(IpAddr::V4("0.0.0.0"), 1337);
axum::Server::bind(&socket)
.serve(router.into_make_service())
.await
.unwrap()
} This is the error:
What am I missing here? Is the expectation that we need to implement these traits by hand if we use a custom state? |
Your If you are still having trouble, please open a new Discussion instead of posting to a merged PR. |
Alright this is hopefully the final change to state stuff before 0.6.0 🤞
This changes
Router::with_state
toi.e. you get back a new
Router
and you get decide what state it needs.This means you can now
nest
andmerge
routers with different state by callingwith_state
:Additionally
Router<()>
now implementsService
andRouterService
has been removed. This means libraries (such as tonic) can now more easily exposeService
compatible interfaces without providing their own.into_service()
- as was necessary before (see hyperium/tonic#1155.)If you do
Router::new().with_state(A).call(request)
the state of the new router is inferred to()
and it all works nicely.Note that
MethodRouter
gets the same treatment.The only downside is that
call
ing a router wherewith_state
has not been called (because it doesn't need any state) doesn't give us a chance to convert everything intoRoute
s ahead of time. That means we need to do it for each request. I haven't measured the perf impact of this but it will at least cause some additional allocations.Router::into_make_service
works around this so it shouldn't impact most users but I've still documented it.Fixes #1547