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

AlmostOnceCell #133

Closed tkaitchuck closed 3 years ago

tkaitchuck commented 3 years ago

As an alternative to https://github.com/matklad/once_cell/issues/61 or https://github.com/matklad/once_cell/issues/53 in the case of a no_std environment where lazy initialization is needed, it would be useful to have an implementation which did not block (and hence depend on locking) or spin, but simply re-ran the initialization logic.

So if the initialization has been completed the existing result is used. However if init is attempted from multiple threads concurrently, then it just gets run more than once. For cases where the init is deterministic this provides a clean, zero-dependency, solution.

matklad commented 3 years ago

your wish is granted https://docs.rs/once_cell/1.5.2/once_cell/race/index.html

tkaitchuck commented 3 years ago

😀

matklad commented 3 years ago

BTW, could you do an API review for the race module? It currently is behind unstable flag; if API looks good to you, I’ll stabilize it

On Tuesday, 12 January 2021, Tom Kaitchuck notifications@github.com wrote:

Closed #133 https://github.com/matklad/once_cell/issues/133.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/matklad/once_cell/issues/133#event-4193847671, or unsubscribe https://github.com/notifications/unsubscribe-auth/AANB3MYTIWZZ32QALMYUBILSZPATFANCNFSM4V56QVHQ .

tkaitchuck commented 3 years ago

Well it does look inconsistent with the others. I don't know if there is a reason for that. Unsync has Lazy and OnceCell, Sync has Lazy and OnceCell, but Race has OnceBox, OnceNonZeroUsize and OnceBool.

OnceBox appears to be the same as OnceCell in the others but is not named similarly. (It's docs look copied and pasted and make no mention of races). Lazy is missing. I don't know if there is a reason for that. But if not it is useful and would be good to add. The other two classes I haven't considered as they don't fit my app's needs.