Feature Proposal: Add `try_clone` method to `Body` trait
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
- Issue type
- Feature
- Clarity
- Mostly clear
- Activity status
- Quiet
- Tech stack
- rust
- Domain
- api, backend-api-design
Research direction
The issue identifies the Body trait as the entry point; read its current definition and implementations, then inspect the retry and redirect use cases named here. Confirm the API shape, delegation expectations, and coverage needed for successful cloning versus unsupported bodies before considering the proposal complete.
Written by the indexing model from the issue text.
Description
Description
When building HTTP clients with tower, tower-http, http-body, we often need to reuse the body data—most commonly for:
- Retrying requests after transient errors
- Following 3xx redirects
- Others...
It is awkward now when working with something like tower::retry, because any middleware is free to wrap the original body in a new type that does not implement Clone.
Once the concrete type is erased (e.g. BoxBody), it forces users to add an extra warpper (“clone adapter”) to make it compile, or to buffer the entire body data, neither of which is ideal.
Proposal
Add a default method to the Body trait:
/// Try to clone this body if the underlying implementation supports it.
///
/// Returns `Some(cloned)` if the entire body can be reproduced verbatim, `None` otherwise.
///
/// Implementations that wrap another body must delegate this call so that the capability is preserved through arbitrary middleware.
fn try_clone(&self) -> Option<Self>
where
Self: Sized,
{
None
}
- No breaking change—every existing type continues to compile, and works as before.
- Middleware that only wrap the inner body can forward the call in one line.
- Retry/redirect policies can attempt try_clone() first and only fall back to buffering when it returns None, keeping the fast path zero-copy.
Alternatives considered
rewind() would also work, which reset the body stream to begining. it good for seekable I/O like files maybe.
/// Return value tell is if rewind success.
///
/// For example: body construct from short `String`, `Vec<u8>`, `Bytes`,... always return true, and retry goes on.
/// body construct from channel/stream, return false, so that retry can not continue.
fn rewind(&mut self) -> bool {
false
}
However, I believe try_clone is more general and the better choice.
- Dominant language
- Rust
- Stars
- 173
- Forks
- 71
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 1
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from hyperium/http-body
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Difficulty 4/5 3-5 days Newbie friendliness 38/100
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
Empty's Body impl `Error = Infallible` makes `Empty::new()` unable to be used w/ Body constraintsOpen
Difficulty 4/5 3-5 days Newbie friendliness 35/100
All issues in hyperium/http-body
Similar issues
-
An EXE started from the shell with MAXALLOC FFFFh leaves a 0-paragraph free block after its ownOpenbug low-priority
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
dividebysandwich/rust-dos#629 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
eliasstravik/herdr-projects#758 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Maintainers usually reply within 1 day
-
enhancement needs:sonnet ready-for-agent
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
kinqsradio/orbyn-engineering#297 ·
Maintainers usually reply within 1 day