Once_cell 0.1.1: a lazy_static on stable Rust without macros


#1

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: https://docs.rs/once_cell/.


#2

Looks interesting. Is there any proof/reasoning/argument that justifies the use of unsafe to show that it introduces no UB?


#3

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 :slight_smile: