HeaderValue::from_static can be const#481
Conversation
78d6c93 to
cedec61
Compare
|
Could you link any rustc tracking issue that stabilizes Also it would seem that this should also include making |
|
@dekellum Just linked this in the PR description: rust-lang/rust#51999 (comment) I looked at |
961cfe1 to
b6c783b
Compare
|
Re: |
|
I'd like to get this merged in if the crate can stomach a higher MSRV. If not, what policy are we aiming for concerning MSRV? |
seanmonstar
left a comment
There was a problem hiding this comment.
I think incrementing the MSRV is fine (as long as it's older than 6 months).
My personal nit is that the panic message here is much more cryptic. I know it points at the line and a user can go read the comment, but this nit personally makes me lean towards waiting for const-panics. I can be overruled if many think it's worth it, though.
src/header/value.rs
Outdated
| #[inline] | ||
| pub fn from_static(src: &'static str) -> HeaderValue { | ||
| #[allow(unconditional_panic)] // required for the panic circumventon | ||
| #[track_caller] |
There was a problem hiding this comment.
Why are we adding track_caller?
There was a problem hiding this comment.
I believe this will provide a more useful line number when the panic occurs. Instead of pointing at hyper's code, it will point at the user's HeaderValue::from_static line
There was a problem hiding this comment.
Does removing this attribute make the panic message much worse?
There was a problem hiding this comment.
Just tried without and it doesn't seem to change anything. Removed!
I agree. I don't love it, but in practice that will almost never happen unless somebody passes a non-ascii |
|
I think it's reasonable to have a slightly-suboptimal panic message until const panic is stable. Given your statement, that's still 6+N months away from In favor of merging as-is. |
|
@LucioFranco @hawkw @carllerche @sfackler @nox opinion on my nit/doing this now with a worse panic message vs waiting? |
|
No need to wait IMO. |
|
Is there an example of what the panic looks like? |
|
Just triggered it with an invalid header value: |
|
Yeah that error is quite cryptic but I think if we add some docs about it on the method then its likely people will find an explanation there and we can add a proper message later when that lands on stable. I don't think this is crazy enough to wait for it, so lets ship it w/ the doc comment? |
|
I've added a panic example, not entirely sure this is the right way to document this though! |
|
Looks like somebody needs to approve the workflow for it to run and test my changes. |
|
@jeromegn I just clicked the button. |
|
I'm not sure how to best present the example panic error. Looks like rustdoc tries to parse it as code and errors. I'm not all that familiar with rustdoc, anybody have any insight? Maybe I don't need an example panic. |
|
You might want to try this: See https://doc.rust-lang.org/rustdoc/documentation-tests.html#attributes. |
|
Can somebody press the approve CI run button :) |
|
I clicked the button but I don't actually have the rights to make the CI start hah. |
* HeaderValue::from_static can be const * bump MSRV to 1.46.0 for const loop
We have a few (lazy) statics for headers we reuse, e.g.
It dawned on me
HeaderValue::from_staticmight beconstwith a few tweaks. With some help in avoiding thepanic!(currently unstable, but stabilization incoming), I was able to make it work.This change would allow us to just write:
It doesn't give a nice panic like before, but if the user follows the backtrace, they should see the comment explaining that a panic here means they've provided an invalid header value.