Hi!
I've recently realized that it is already possible to implement lazy_static!
(and more!) on stable Rust with a nice API without macros.
So, once_cell was created, which implements a single assignment cell-like type, which you can just use in a static context, without any strings attached.
See the docs for various examples: once_cell - Rust.
4 Likes
Looks interesting. Is there any proof/reasoning/argument that justifies the use of unsafe to show that it introduces no UB?
Is there any proof/reasoning/argument that justifies the use of unsafe to show that it introduces no UB?
There's definitely no formal proof. The informal reasoning is documented in the source code, and I would really appreciate a unsafe code review: https://github.com/matklad/once_cell/issues/1.
The implementation is mostly based on lazycell
and lazy_static
, which is not as reassuring as it sounds, because both of them had/have soundness holes 
https://github.com/indiv0/lazycell/pull/74
https://github.com/rust-lang-nursery/lazy-static.rs/issues/105
2 Likes