I just turned arg into a &str it shouldn't make a difference what type it is as long as its not generic (Self, T, ect). To use lazy_static with a fn call it has to be an argument-less fn like HashMap::new() because of the way the macro expands. playground
Not sure if this will be helpful but here is the expanded version
But this will return a newly allocated &'static Self every time you call get_static; it won't return a reference to the same one.
The semantics of lazy_static!, if they did work here, suggest that you want to store whatever value was calculated the first time get_static was called, and all subsequent calls to get_static would return references to that value, ignoring arg. Is that what you want? I think you could do that in basically the same way lazy_static works, basically using an Option that is lazily initialized and then using unsafe to create a &'static to the inside.
You can even handle generics, with a different value for each type, using typemap (but only for types that are 'static -- lifetimes are a compile time construct and typemaps work at run time).
Or do you want get_static to look up arg in a table of some kind and return a different &'static Self for each value of arg, but the same reference if you call it with the same arg twice?
I don't know what you mean by "igoring arg", but otherwise, yes, that's what I want.
I think you could do that in basically the same way lazy_static works, basically using an Option that is lazily initialized and then using unsafe to create a &'static to the inside.
Meaning, if you call get_static("foo") and then get_static("bar"), only "foo" will ever be passed to Self::from_somewhere because the second get_static call will just return a reference to the one that was created last time. This isn't necessarily wrong, but it seems pretty weird, to me -- what's the point of passing an argument every time if it only matters once? Not to mention, if you add threads it also becomes racy. Ugh.
Sorry, I tried, but failed. Knowing the theory does not in this case translate to knowing how to write the code. Maybe I'll give it another shot when I get a chance; I haven't used OnceCell before.