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 thread local Cell methods #92122

Closed
4 tasks done
m-ou-se opened this issue Dec 20, 2021 · 9 comments · Fixed by #114689
Closed
4 tasks done

Tracking issue for thread local Cell methods #92122

m-ou-se opened this issue Dec 20, 2021 · 9 comments · Fixed by #114689
Labels
C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@m-ou-se
Copy link
Member

m-ou-se commented Dec 20, 2021

Feature gate: #![feature(local_key_cell_methods)]

This is a tracking issue for thread local Cell methods: RFC 3184.

Public API

impl<T: 'static> LocalKey<Cell<T>> {
    pub fn set(&'static self, value: T);
    pub fn get(&'static self) -> T where T: Copy;
    pub fn take(&'static self) -> T where T: Default;
    pub fn replace(&'static self, value: T) -> T;
}

impl<T: 'static> LocalKey<RefCell<T>> {
    pub fn with_borrow<F, R>(&'static self, f: F) -> R where F: FnOnce(&T) -> R;
    pub fn with_borrow_mut<F, R>(&'static self, f: F) -> R where F: FnOnce(&mut T) -> R;
    pub fn set(&'static self, value: T);
    pub fn take(&'static self) -> T where T: Default;
    pub fn replace(&'static self, value: T) -> T;
}

Steps / History

Unresolved Questions

  • Is the behavior of set() (to skip the default initialization) the right behavior? Yes.
  • Are there any other types commonly used as thread locals for which we should do something similar?
    • Do we also want anything for UnsafeCell? Maybe LocalKey<UnsafeCell<T>>::get() to get the *mut T, just like UnsafeCell<T>::get().
    • Not a blocker for stabilizing this feature. Methods for other types can be a separate feature.
@m-ou-se m-ou-se added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. labels Dec 20, 2021
bors added a commit to rust-lang-ci/rust that referenced this issue Mar 5, 2022
…joshtriplett

Implement RFC 3184 - thread local cell methods

This implements [RFC 3184](rust-lang/rfcs#3184), with `@danielhenrymantilla's` [suggestion](rust-lang/rfcs#3184 (comment)) for the `with_` method names.

Tracking issue: rust-lang#92122
@dymayday
Copy link

Hello,
Can it be stabilized ?

@m-ou-se m-ou-se added the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Jul 27, 2023
@m-ou-se
Copy link
Member Author

m-ou-se commented Jul 27, 2023

Nominated this for discussion in the next libs-api meeting.

I believe the only remaining point of discussion is the behaviour of .set():

The .set() method bypasses the lazy initialization that .with(|v| v.set()) would perform, making this pattern possible:

thread_local! {
    static ID: Cell<i32> = panic!("ID not set on this thread!");
}

fn main() {
    ID.set(123); // OK, no panic.
    println!("{}", ID.get()); // OK, no panic now that it has been set.
}

@dymayday
Copy link

Awesome ! And thank you for your answer.

@m-ou-se
Copy link
Member Author

m-ou-se commented Aug 1, 2023

@rfcbot merge

@rfcbot
Copy link

rfcbot commented Aug 1, 2023

Team member @m-ou-se has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Aug 1, 2023
@rfcbot
Copy link

rfcbot commented Aug 1, 2023

🔔 This is now entering its final comment period, as per the review above. 🔔

@m-ou-se m-ou-se removed the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Aug 8, 2023
@tgross35
Copy link
Contributor

Nominated this for discussion in the next libs-api meeting.

I believe the only remaining point of discussion is the behaviour of .set():

Was there any specific resolution on this? (I have no qualms, just wondering for completeness)

@m-ou-se
Copy link
Member Author

m-ou-se commented Aug 10, 2023

Initially, not everyone was convinced this was a good idea. Some thought that making .set() not do the same as .with(|x| x.set()) can result in subtle bugs. But after some discussion, we couldn't think of common situations where that'd easily result in bugs. We discussed some valuable use cases that are enabled by the proposed behaviour of .set(). Eventually we concluded that the proposed behaviour is acceptable and sometimes very useful, on the condition that the behaviour is well documented (which it already is).

@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. to-announce Announce this issue on triage meeting and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Aug 11, 2023
@rfcbot
Copy link

rfcbot commented Aug 11, 2023

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

This will be merged soon.

@bors bors closed this as completed in 656ee47 Aug 16, 2023
@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Aug 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. 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