matklad / once_cell

Rust library for single assignment cells and lazy statics without macros
Apache License 2.0
1.87k stars 109 forks source link

Allow reentrant initialization of unsync::OnceCell #62

Closed pitdicker closed 5 years ago

pitdicker commented 5 years ago

This is not really a serious pull request, but I am interested in your thoughts.

I don't think it is necessary for the unsync variant of OnceCell to panic on reentrant initialization: because there are no other references to its interior when the result is written by get_or_init, the double write can not be observed outside the initializer, and no aliasing guarantees are violated.

Still a panic may be useful because this may indicate an error, and because it gives some symmetry to the sync variant.

matklad commented 5 years ago

I believe this is unsound, see a new test in https://github.com/matklad/once_cell/pull/63

pitdicker commented 5 years ago

You are right, it is possible for the closure to sneak out a reference. Good test! Do you think it is useful to document the reason(s) reentrant initialization is not allowed?

matklad commented 5 years ago

Good call: https://github.com/matklad/once_cell/pull/64