matklad / once_cell

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

Split out synchronization from is_initialized #74

Closed pitdicker closed 2 years ago

pitdicker commented 4 years ago

This is based on top of https://github.com/matklad/once_cell/pull/72, only the last two commits are relevant.

Before using MaybeUninit there were two ways to check whether a OnceCell was initialized:

  1. call is_initialized, which had the side effect of acquiring the value.
  2. test if the option was Some, which didn't do any synchronization.

After https://github.com/matklad/once_cell/pull/72 we just always call is_initialized. I would like to bring back the distinction:

  1. fn sync_get will check if it is initialized, acquire and return the value
  2. fn is_initialized will only check if it is initialized, without synchronizing.

This is the only change I would need to the sync module to be able to add an experimental implementation using consume. Do you think it makes sense on its own? And is the change small enough not to bother duplicating the sync module?

The last commit is optional. It seems nice if initialize would return value. This prevents an unnessecary unwrap in get_or_try_init, for which the code used an unsafe get_unchecked before to prevent.