axum: allow body types other than axum::body::Body
in Service
s passed to serve
#3205
+61
−19
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
A user in #3202 tried to use middlewares around the whole
Router
so that their layers run before routing happens. They ran into problems caused by their layers changing theBody
type.So far I've seen only the
NormalizePathLayer
used in this way which does not change theBody
type so there were never issues. But I don't see a reason not to support this for other layers that do change the body, such asCompressionLayer
.Solution
This PR adds another generic parameter to
Serve
to keep track of theResponse::Body
type returned by the service provided.There are no breaking changes as far as I can tell as inference knows the body type. This could break someone who for some reason names the type parameters explicitly. There is no rush with this and we may want to wait with this and release it in the next breaking release.
Alternatives
We may also choose not to support this. Users can just map the response body with
.map_response_body(axum::body::Body::new)
before callignserve
. The price is just one box (and maybe a bit of ergonomics) as far as I can tell.