hyperium / headers

Typed HTTP Headers from hyper
https://hyper.rs
MIT License
162 stars 84 forks source link

Creating a &'static HeaderName #143

Closed zesterer closed 1 year ago

zesterer commented 1 year ago

Hi,

The Header trait requires a name function that returns a &'static HeaderName. However, none of the methods used to construct a HeaderName are marked const (and so cannot be used to create a regular static, either explicitly or via promotion of a local temporary), so there appears to only be two possible ways to write a valid implementation of Header::name:

1) Box and then leak a HeaderName on every call

2) Set up a lazy_static (or equivalent via new primitives like LazyCell) that uses internal mutability to lazily initiate a HeaderName

Both of these approaches are undesirable for fairly clear reasons, the first especially, which leads me to the question:

What is the intended approach when implementing this trait?

Or, alternatively, would it be possible to do one of the following? 1) Make some of the methods used to create `HeaderName` `const` (as is the case for `HeaderValue`) 2) Have `Header::name` return `Cow<'static, HeaderName>`
seanmonstar commented 1 year ago

The expected options are:

zesterer commented 1 year ago

Unfortunately, only the lazy_static route is available to me right now. I guess it'll have to do.