Custom headers in hyper 0.12

Hey guys!

Just a quick question. It seems there is no more header! macro in hyper 0.12. So what is the new fancy way to create custom headers then?

Sorry if the answer is obvious and I’ve missed it somehow, I’m just a noob.

The typed header interface in hyper 0.11 and older isn’t there anymore. The hyperx crate has an extracted copy that you can use:

@sfackler, thank you for your help, but I wonder how it is supposed to be done in hyper 0.12.

Afterall I don’t think @seanmonstar would leave us without any solution at all, just hoping someone would pool the old typed header module into yet another crate.

Assuming that you now need to create a custom http crate HeaderName (e.g. for appending to an http::HeaderMap) then there is no equivalent to the hyper (now available in hyperx) header! macro. I’ve been doing it with a static byte slice for the name:

pub static META_RES_DECODED: &[u8] = b"response-decoded";

See also:

@dekellum, thank you for your method, though why declare as a pub static? Would that be any better or faster than just parsing it from &str like so?

let mut headers = HeaderMap::new();

headers.insert("hello".parse::<HeaderName>().unwrap(), "world".parse().unwrap());

The static just ensures one original byte sequence compiled, though its also copied into the HeaderMap on each insert at runtime. pub for the lib export. I haven’t benchmarked from_lowercase(&[u8]) vs the more general parse(&str), but its probably not much difference. HeaderName::from_static might avoid the copy, now that that is available.

1 Like