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
Typed Headers? #264
Comments
The header implementations are generally useful. I think it would be valuable to provide them as part of
most libraries will only depend on the core types. This strategy is being adopted to allow breaking change iterations while maintaining stability of core types. |
+1 to @carllerche's plan. The other thing that I'd like—and this is mostly optional/depends on stability of functionality in the headers crate—is the ability to define custom headers without waiting for additional features in |
You should be able to use lazy_static to make 'static HeaderName instances for custom headers. |
See also #174, which included an example (and slightly weird syntax requirement) with lazy_static. |
@sfackler the trait was adjusted to have |
Oh yeah, that would be a problem. Is the name actually used in a const context? |
Well, it might be useful for me to explain why I care about this feature. Namely—I'd like to have typed versions of the headers the AWS Lambda Runtime API returns without all sorts of stringly-typed interfaces. Like I said previously, I'd be happy to opt into some sort of unstable interface/feature flag so that our customers can interact with typed values. |
I agree, it would be nice to have a way to define custom static headers. I think we can do this with a macro, but it will require bytes to provide a macro to define a static bytes struct as well. |
I think I have the capacity to tackle this. Would the macro wrap the |
Why would Bytes need a macro? I would think we could eventually make |
My understanding is that string inspection + panicking if invalid is blocked on rust-lang/rust#49146 and rust-lang/rust#51999, which don't have too much traction at the moment. That seems to be the correct solution to me, but I don't know when to expect it. |
So, I tried to create a macro that'd create a header, but I don't think it's possible to do so. Here's the code I wrote: macro_rules! const_header {
($header_name:tt) => {
&HeaderName::from_static($header_name)
}
}
struct AWSRequestId(String);
impl Header for AWSRequestId {
const NAME: &'static HeaderName = const_header!("LAMBDA_RUNTIME_AWS_REQUEST_ID");
fn decode<'i, I>(values: &mut I) -> Result<Self, HeaderError> where
Self: Sized,
I: Iterator<Item=&'i HeaderValue> {
unimplemented!()
}
fn encode<E: Extend<HeaderValue>>(&self, values: &mut E) {
unimplemented!()
}
} ...and here's the error message:
Looking more at the |
Sorry to revive this thread in a strange way, but I would like to point out a clippy lint when using The following code: pub const NAME: HeaderName = HeaderName::from_static("test"); Produces the following warning:
I could just ignore the issue and use First of all, the reason behind the lint: there is an
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 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 Do you have better ideas? Do you think that this should be handled in a dedicated issue? |
Sounds to me like the clippy lint is wrong. |
I went through the discussion for the lint, because this was exactly my initial thought. However, after reading the issues related to the implicit copies related to the interior mutability when working with consts, I changed my mind and I think that the clippy lint is right instead. Let me make a counter-example: if you change from |
I'd recommend moving this to a different issue, so that this one can stay on "typed headers". |
Work has been progressing on a typed headers system to replace what hyper had prior to v0.12. Some questions arise as to where this stuff should "live".
headers
crate name, and has agreed to transfering the name if we wish to use it for this.http-headers
crate name, and has suggested we could publish there, as the nameheaders
might be too vague.http
.let conlen = http::header::ContentLength(450);
, and perhaps it should be the eventual goal.http
was to have a stable crate that the ecosystem can use to be "generic" over HTTP implementations. Therefore, breaking changes must be exceptional. I have taken much effort to reduce the exposed surface area in the new crate API, specifically to reduce the need for breaking changes. Still, I would have to have missed something and be stuck with it.http
API could be moved tohttp-core
and re-exported, and have the stability be stronger onhttp-core
thanhttp
.See also #136.
The text was updated successfully, but these errors were encountered: