When using HeaderName::from_static in in order to create aconst(instead of astatic` variable), clippy triggers a warning. Indeed, the following code:
pub const NAME: HeaderName = HeaderName::from_static("test");
Produces the following warning:
warning: a const item should never be interior mutable
Playground
I could just ignore the issue and use static instead of const, but I think that the lint exposes an interesting fact I would like to discuss.
First of all, the reason behind the lint: there is an AtomicPtr deep down. The dependency chain is:
HeaderName -> Repr<Custom> -> Custom -> ByteStr -> Bytes -> AtomicPtr
Now, the issue itself... It is a non-problem, except for one detail: the fact that potentially we have internal mutability in a header string that obviously is meant to never change is unexpected. Don't get me wrong, I understand the reason behind this: we want to be able to reuse memory when receiving headers (thanks to Bytes) and at the same time it is extremely nice to have just one HeaderName abstraction (instead of HeaderName and ConstHeaderName, for instance). However, following the principle of least surprise, probably you would expect a const HeaderName::from_static() function to not have interior mutability.
Now, what should be done? To be honest, I am not sure. I personally don't see a practical problem with the current approach, which is pretty ergonomic. Maybe we could consider adding a note in the documentation of HeaderName::from_static, suggesting to use a static variable instead of a const to avoid being flooded by clippy warnings.
Do you have better ideas?
CC @paolobarbolini
Originally posted by @dodomorandi in #264 (comment)
When using
HeaderName::from_staticinin order to create aconst(instead of astatic` variable), clippy triggers a warning. Indeed, the following code:Produces the following warning:
Playground
I could just ignore the issue and use
staticinstead ofconst, but I think that the lint exposes an interesting fact I would like to discuss.First of all, the reason behind the lint: there is an
AtomicPtrdeep down. The dependency chain is:Now, the issue itself... It is a non-problem, except for one detail: the fact that potentially we have internal mutability in a header string that obviously is meant to never change is unexpected. Don't get me wrong, I understand the reason behind this: we want to be able to reuse memory when receiving headers (thanks to
Bytes) and at the same time it is extremely nice to have just oneHeaderNameabstraction (instead ofHeaderNameandConstHeaderName, for instance). However, following the principle of least surprise, probably you would expect aconst HeaderName::from_static()function to not have interior mutability.Now, what should be done? To be honest, I am not sure. I personally don't see a practical problem with the current approach, which is pretty ergonomic. Maybe we could consider adding a note in the documentation of
HeaderName::from_static, suggesting to use astaticvariable instead of aconstto avoid being flooded by clippy warnings.Do you have better ideas?
CC @paolobarbolini
Originally posted by @dodomorandi in #264 (comment)