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

Create header name/value from String without reallocating #1295

Closed
glueball opened this issue Aug 3, 2024 · 0 comments · Fixed by #1296
Closed

Create header name/value from String without reallocating #1295

glueball opened this issue Aug 3, 2024 · 0 comments · Fixed by #1296
Labels
proposal Enhancement idea or proposal

Comments

@glueball
Copy link
Contributor

glueball commented Aug 3, 2024

Proposed change

Implement IntoHeaderName and IntoHeaderValue for an (owned) String, so no allocation is necessary (*).

(*) For HeaderName, the underlying does not allocate if the Vec has not leftover capacity (https://docs.rs/bytes/latest/src/bytes/bytes.rs.html#849-882), but still this is the best we can do. However, I assume that for HeaderName using &'static str is the more common case.

Use case

I have to use format! to build some complex HeaderValue. So far, I have to use my_value.as_str().into_header_value() which internally uses .to_string(), so it creates a new allocation for the string, and the old allocation gets just freed afterward. Maybe the compiler can optimize this allocation away, but maybe not...

Contribution

Yes. Preparing PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal Enhancement idea or proposal
Projects
None yet
1 participant