feat(body): add UnsyncBoxBody::new constructor and From<ResponseBody>#682
Open
jlizen wants to merge 1 commit into
Open
feat(body): add UnsyncBoxBody::new constructor and From<ResponseBody>#682jlizen wants to merge 1 commit into
jlizen wants to merge 1 commit into
Conversation
Member
Author
|
@seanmonstar / @davidpdrsn - either of y'all have time to review this? |
af4617f to
3292358
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Supersedes: #537
Motivation
Users who return responses with different body types (e.g., from multiple match branches) need to box them into a common type. When one branch uses
ServeDir/ServeFile, the response body is already internally boxed, but wrapping it inUnsyncBoxBody::new()causes double-boxing (double allocation + double indirection).PR #537 by @nanoqsh added
Fromimpls directly onhttp_body_util::combinators::UnsyncBoxBody, but tower-http has a policy of not leakinghttp_body_utiltypes in its public API.Solution
Expose tower-http's own
UnsyncBoxBodywrapper with:new()constructor that type-erases anyBodyimpl (analogous tohttp_body_util::combinators::UnsyncBoxBody::new)From<ServeFileSystemResponseBody>impl that unwraps the already-boxed inner body without re-boxingUsage becomes:
The internal
pub(crate) fn new()that previously accepted the rawhttp_body_utilinner type was renamed tofrom_inner()to avoid ambiguity with the new public constructor.Tests verify that
UnsyncBoxBody::newcorrectly boxes a body and that theFrom<ResponseBody>conversion produces a valid, readable body from a realServeDirresponse. The conversion is zero-cost by construction (it just moves the inner field), so correctness is primarily a compile-time guarantee.