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

Expected struct <FOOBAR>, found <OTHER_LAZY_STATIC_REF> #119

Closed
jens1o opened this issue Sep 7, 2018 · 4 comments
Closed

Expected struct <FOOBAR>, found <OTHER_LAZY_STATIC_REF> #119

jens1o opened this issue Sep 7, 2018 · 4 comments

Comments

@jens1o
Copy link

jens1o commented Sep 7, 2018

lazy_static! {
    static ref FIREFOX_REGEX: Regex = { Regex::new(r"firefox/([\d\.]+)").unwrap() };
    static ref EDGE_REGEX: Regex = { Regex::new(r"edge/(\d{2}\.\d+)").unwrap() };
    static ref BROWSER_LIST: HashMap<&'static str, Regex> = {
        let m: HashMap<&'static str, Regex> = HashMap::new();

        m.insert("Firefox", FIREFOX_REGEX);
        // -----------------^^^^^^^^^^^^^
        // expected struct Regex, found struct FIREFOX_REGEX
        m.insert("Edge", EDGE_REGEX);
        // --------------^^^^^^^^^^
        // expected struct Regex, found struct EDGE_REGEX

        m
    };
}

I don't understand that.
I don't understand that.

@KodrAus
Copy link
Contributor

KodrAus commented Sep 8, 2018

Hi @jens1o! the problem here is that each static ref is actually lying about its type. So FIREFOX_REGEX and EDGE_REGEX aren't Regexes, they're some type that we can dereference to a Regex. To add them to your browser list we need to dereference them. So with some tweaking we can make your example work:

lazy_static! {
    static ref FIREFOX_REGEX: Regex = { Regex::new(r"firefox/([\d\.]+)").unwrap() };
    static ref EDGE_REGEX: Regex = { Regex::new(r"edge/(\d{2}\.\d+)").unwrap() };
    static ref BROWSER_LIST: HashMap<&'static str, &'static Regex> = {
        let mut m = HashMap::new();

        m.insert("Firefox", &*FIREFOX_REGEX);
        m.insert("Edge", &*EDGE_REGEX);

        m
    };
}

@jens1o
Copy link
Author

jens1o commented Sep 8, 2018

Oh okay. Is it possible to make the error message more clearer? Thanks!

@KodrAus
Copy link
Contributor

KodrAus commented Sep 8, 2018

It's a bit confusing, isn't it. I think the best way we could improve the error messages here is by requiring the actual type name in the static. That way you know what you're looking at. This is something @matklad's alternative API for statics with complex initialisation does.

@KodrAus
Copy link
Contributor

KodrAus commented Jan 3, 2019

Thanks again for the input @jens1o!

I'll go ahead and close this one now as something that's solved by #111, depending on what direction that goes in.

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