Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tracking issue for std::sync::Once::is_completed #54890

Closed
SimonSapin opened this issue Oct 7, 2018 · 5 comments · Fixed by #68945
Closed

Tracking issue for std::sync::Once::is_completed #54890

SimonSapin opened this issue Oct 7, 2018 · 5 comments · Fixed by #68945
Labels
B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@SimonSapin
Copy link
Contributor

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 the T only once. Because I can't check if Once is initialized, I am forced to add an indirection and check the value of ptr instead:

https://github.com/matklad/once_cell/blob/8127a81976c3f2f4c0860562c3f14647ebc025c0/src/lib.rs#L423-L429

https://github.com/matklad/once_cell/blob/8127a81976c3f2f4c0860562c3f14647ebc025c0/src/lib.rs#L457-L461

The parking_lot's version of Once 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.

@SimonSapin SimonSapin added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC labels Oct 7, 2018
@nagisa
Copy link
Member

nagisa commented Oct 7, 2018

The documentation needs to be made slightly more strict.

For example:

It is also possible that immediately after is_completed returns false, some other thread finishes executing call_once.

Should be replaced with

This function returning false does not mean that Once has not been executed.

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
Copy link
Contributor Author

CC @matklad for nagisa’s comment above.

pietroalbini added a commit to pietroalbini/rust that referenced this issue Oct 9, 2018
Fix tracking issue for Once::is_completed

rust-lang#53027 was merged without a tracking issue. I just filed rust-lang#54890. CC @matklad
pietroalbini added a commit to pietroalbini/rust that referenced this issue Oct 10, 2018
Fix tracking issue for Once::is_completed

rust-lang#53027 was merged without a tracking issue. I just filed rust-lang#54890. CC @matklad
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this issue Oct 11, 2018
Fix tracking issue for Once::is_completed

rust-lang#53027 was merged without a tracking issue. I just filed rust-lang#54890. CC @matklad
kennytm added a commit to kennytm/rust that referenced this issue Oct 12, 2018
Fix tracking issue for Once::is_completed

rust-lang#53027 was merged without a tracking issue. I just filed rust-lang#54890. CC @matklad
@mjbshaw
Copy link
Contributor

mjbshaw commented Jul 10, 2019

It's been 9 months since this was implemented. Can this be stabilized now?

@pyrrho
Copy link

pyrrho commented 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
Copy link
Member

matklad commented 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 of Once 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.

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Feb 20, 2020
…albertodt

Stabilize Once::is_completed

Closes rust-lang#54890

This function has been around for some time. I haven't seen anyone raise any objections to it. I've personally found it useful myself. It would be nice to finally stabilize it and
@bors bors closed this as completed in 588f008 Feb 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants