Unresolved import when calling a macro defined in an external crate

泄露秘密 提交于 2019-12-13 21:10:05

问题


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


回答1:


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!



来源:https://stackoverflow.com/questions/28526180/unresolved-import-when-calling-a-macro-defined-in-an-external-crate

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!