Closed
Description
Implemented in #53027
I propose to expose a way to check if a
Once
instance is initialized.I need it in
once_cell
.OnceCell
is effetively a pair of(Once, UnsafeCell<Option<T>>)
, which can set theT
only once. Because I can't check ifOnce
is initialized, I am forced to add an indirection and check the value of ptr instead:The
parking_lot
's version ofOnce
exposes the state as an enum: https://docs.rs/parking_lot/0.6.3/parking_lot/struct.Once.html#method.state.I suggest, for now, just to add a simple
bool
function: this fits my use-case perfectly, exposes less implementation details, and is forward-compatible with more fine-grained state checking.
Activity
nagisa commentedon Oct 7, 2018
The documentation needs to be made slightly more strict.
For example:
Should be replaced with
The distinction is important, because at the exact moment this function returns
Once
might already be executed/resolved, without it necessarily happening “immediately after”.To avoid people shotgunning their feet off, I would suggest to add some guidance on how this should be used. For example, doing something useful based on this function returning a
false
is somewhat likely to be a bug. And yet, examples do not really result in such an impression.SimonSapin commentedon Oct 7, 2018
CC @matklad for nagisa’s comment above.
Rollup merge of rust-lang#54891 - rust-lang:SimonSapin-patch-1, r=nagisa
Rollup merge of rust-lang#54891 - rust-lang:SimonSapin-patch-1, r=nagisa
Rollup merge of rust-lang#54891 - rust-lang:SimonSapin-patch-1, r=nagisa
Rollup merge of rust-lang#54891 - rust-lang:SimonSapin-patch-1, r=nagisa
mjbshaw commentedon Jul 10, 2019
It's been 9 months since this was implemented. Can this be stabilized now?
pyrrho commentedon Oct 31, 2019
Is there anything preventing this method from being stabilized? It looks like there was some bikesheding in #53027 but it's now been just past a year since initial adoption.
matklad commentedon Oct 31, 2019
Note that I am personally no longer directly interested in having this method: in
once_cell
, I ended-up copy-pasting the whole ofOnce
anyway. That said, this does look like straightforward addition, which allows to do more with the thing, without actually expanding it's surface area greatly.3 remaining items