Skip to content
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

How to use the vis specifier inside the lazy_static! macro? #146

Open
magiclen opened this issue Apr 30, 2019 · 1 comment
Open

How to use the vis specifier inside the lazy_static! macro? #146

magiclen opened this issue Apr 30, 2019 · 1 comment

Comments

@magiclen
Copy link

When compiling the code below

#[macro_use]
extern crate lazy_static;

macro_rules! unit_like_struct {
    ( $v:vis $name:ident) => {
        $v struct $name;
    }
}

unit_like_struct!(pub MyStruct);

macro_rules! lazy_string {
    ( $v:vis $name:ident, $s:expr) => {
        lazy_static! {
            $v static ref $name: String = format!($s);
        }
    }
}

lazy_string!(pub NOTHING, "nothing");

Cargo says,

error: no rules expected the token `pub `
  --> src/main.rs:15:13
   |
15 |             $v static ref $name: String = format!($s);
   |             ^^ no rules expected this token in macro call
...
20 | lazy_string!(pub NOTHING, "nothing");
   | ------------------------------------- in this macro invocation

Thanks in advance!

@magiclen magiclen changed the title How to use the vis specifier inside the macro_rules! macro? How to use the vis specifier inside the lazy_static! macro? Apr 30, 2019
@KodrAus
Copy link
Contributor

KodrAus commented May 2, 2019

Hi @magiclen 👋

Unfortunately the lazy_static macro predates proper macro support for pub(restricted) (which appears to have stabilized in 1.30.0) so it rolls its own. This doesn't appear to be compatible with the proper vis matcher. That's a bit nasty though, so we should look at shifting to vis at some point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants