Search code examples
rusthyper

Unresolved import when calling a macro defined in an external crate


I am trying to implement a custom HTTP header using impl_header macro from hyper crate but it seems it cannot resolve the hyper::header module.

Here is my code:

#[macro_use] extern crate hyper;
use hyper::header;
struct CustomHeader;
impl_header!(CustomHeader, "value", String);

And here is the compiler error:

<hyper macros>:11:14: 11:20 error: unresolved import `header::HeaderFormat`. Maybe a missing `extern crate header`?
<hyper macros>:11 Result { use header:: HeaderFormat ; self . fmt_header ( f ) } } }
                               ^~~~~~
<hyper macros>:1:1: 11:67 note: in expansion of impl_header!
lib.rs:4:1: 4:45 note: expansion site
error: aborting due to previous error
Could not compile `macro_issue`.

Any clue why this happens and how can I fix it?

Thank you


Solution

  • I agree with Renato that this is an issue with hyper, and you should file a bug (or even better a pull request!). If you'd like to work around it for now though, you can re-export header as your own:

    #[macro_use]
    extern crate hyper;
    
    pub use hyper::header as header;
    
    struct CustomHeader;
    impl_header!(CustomHeader, "value", String);
    
    fn main() {}
    

    Unfortunately, this simply unlocks a whole new wave of errors that I'll let you figure out!