Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upModernizing LazyStatic APIs #111
Comments
This comment has been minimized.
This comment has been minimized.
|
Here's the proposes |
This comment has been minimized.
This comment has been minimized.
|
I like the direction this is headed in! Ultimately, I'd really like to see something like this in With that said, a dependency on With that said, this is a double edge sword, because I also like the idea of more people using and testing |
This comment has been minimized.
This comment has been minimized.
That is a valid concern. We need Now, rust-lang/rust#52239 literally just removed the So, two questions:
|
This comment has been minimized.
This comment has been minimized.
|
I'm not sure about the In terms of making |
This comment has been minimized.
This comment has been minimized.
Yep, that is correct. OTOH, if you care about number of deps, your deps are probably caring about that as well, and would use I've pushed updates that make parking_lot optional and default, employ the &'static "polyfill" for Once and remove usages of non-essential new features. That gives use compatibility with Rust 1.24.0 an up, with or without parking-lot, on a somewhat questionable ground that pretending that non-static Once is static is OK :) |
This comment has been minimized.
This comment has been minimized.
|
Cool! Do the fields here need to be public? We just finished removing the equivalent from |
This comment has been minimized.
This comment has been minimized.
|
Excellent observation @anp! I can't think of a way to make fields private: because we don't create a fresh type for per lazy instance anymore, we need to store the closure inside a struct, and that should work in What we can do, however, is to hide |
This comment has been minimized.
This comment has been minimized.
|
No, this doesn't entirely fix the hole unfortunately, the user can just overwrite the state directly :( What we can do is to split the |
This comment has been minimized.
This comment has been minimized.
|
Another potential problem with
There's also an extra indirection during intialization, but that probably does not matter, because |
KodrAus
referenced this issue
Sep 8, 2018
Closed
Expected struct <FOOBAR>, found <OTHER_LAZY_STATIC_REF> #119
This comment has been minimized.
This comment has been minimized.
clarfon
commented
Sep 27, 2018
|
I personally wouldn't mind if |
matklad commentedAug 3, 2018
•
edited
Context: https://internals.rust-lang.org/t/pre-rfc-lazy-static-move-to-std/7993/36
Rust has progressed enough to make lazy static API less magical. It could look like this (example from
once_cell):I am creating this issue to discuss what we can do with this exciting possibility :-)
Just to be clear, I am explicitly not suggesting that we should deprecate the current API and switch to the new shiny. There's a ton of code in the wild which uses
lazy_static!, and that is great.Nevertheless, I think the current API has some problems, and the possible new API has less of them! Specifically, the current
lazy_static!macro is opaque: it has a somewhat unique syntax, which is Rustish, but is not exactly Rust, it creates a unique hidden type behind the scenes and the implementation is hard to follow. I think this are significant drawbacks, especially from the learnability perspective.When a new rustecean asks "how can I have global data?", the typical answer is: "you need to lazily initialize data on the first access. This is what C++ and Java do at the language level, but Rust this is achieved via the
lazy_staticlibrary". And then a rustecan goes to see howlazy_staticis implemented, sees this and thinks "wow, this is almost as horrifying as STL implementations" (well, at least that was my reaction :D).I'd want to argue that an explicit
Lazy<T>would be clearer at the call site (no more magical unique types) and much easier to understand (you just Ctrl+B/F12/M-./gD and read the impl).An interesting facet of the new API is that
Lazydoes not need to be static!So, are folks excited about the possibility of getting rid of
lazy_static!macro? I propose the following plan for this:sync::Lazyfromonce_celland publish it as a separate rust-lang-nursery crate,sync_lazy, with the following APIsync_lazycrate usesparking_lot::Once. That way, we don't need to wait until non-staticOnceis stable, and we also giveparking_lotsome more testing, as was requested in rust-lang/rfcs#1632 (hence, cc @Amanieu)In the
lazy_staticreadme, we point that one might trysync_lazyinstead.