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

impl get_mut_or_init and get_mut_or_try_init for OnceCell and OnceLock #114788

Merged
merged 1 commit into from
Apr 6, 2024

Conversation

tisonkun
Copy link
Contributor

See also #74465 (comment)

I'm trying to understand the process for such proposal. And I'll appreciate it if anyone can guide me the next step for consensus or adding tests.

@rustbot
Copy link
Collaborator

rustbot commented Aug 14, 2023

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @cuviper (or someone else) soon.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 14, 2023
@rust-log-analyzer

This comment has been minimized.

@tgross35
Copy link
Contributor

Something like this needs an API Change Proposal (ACP) - you do that by creating an issue on the libs team repo here https://github.com/rust-lang/libs-team (ACP is one of the templates).

@tgross35
Copy link
Contributor

What situation are you trying to solve exactly? (You will need to provide a motivating example in the ACP if you file one). Based on your example on the other thread, this works:

use std::cell::OnceCell;
use std::slice::Iter;

type RecordBatch = i32;

struct Foo {
    batches: OnceCell<Vec<i32>>
}

impl Foo {
    pub fn batches(&self) -> Iter<'_, RecordBatch>  {
        self.batches
            .get_or_init(|| vec![])
            .iter()
    }
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=cd3028158256e22d075a80b05dc476b8

@cuviper
Copy link
Member

cuviper commented Aug 14, 2023

@rustbot label +T-libs-api -T-libs
r? libs-api

@rustbot rustbot added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 14, 2023
@rustbot rustbot assigned m-ou-se and unassigned cuviper Aug 14, 2023
@tisonkun
Copy link
Contributor Author

this works

But the mut version is awkward:

    pub fn mut_batches(&mut self) -> IterMut<'_, RecordBatch> {
        self.batches.get_or_init(|| load_batches(&self.buf));
        // SAFETY - init above
        unsafe { self.batches.get_mut().unwrap_unchecked() }.iter_mut()
    }

@tgross35
Copy link
Contributor

tgross35 commented Aug 16, 2023

Can you post a more complete version of a use case? If you're trying to repeatedly edit data behind & (rather than &mut) then you need a RefCell - RefCell<T>, RefCell<OnceCell<T>> or RefCell<Option<T>> depending on your exact needs.

Also for your example (in case it's coming from real code), I would not use unwrap_unchecked here - it's trivial for the compiler to optimize the check out. You're generally better off always using unwrap instead of unwrap_unchecked without very very good reasoning. It's safer against refactoring, and can often even wind up faster (proving preconditions for optimizations and such...)

@tgross35
Copy link
Contributor

Ah, I see your use case here https://github.com/tisonkun/kafka-api/blob/d080ab7e4b57c0ab0182e0b254333f400e616cd2/kafka-api/src/record.rs#L108-L116. It seems like you are happy with using &mut in your function signature right, and the goal is just to simplify the following?

// Old
oncecell.get_or_init(|| foobar());
oncecell.get_mut().unwrap()

// New
oncecell.get_mut_or_init(|| foobar())

That makes more sense. If you write an ACP, link it here

@tisonkun
Copy link
Contributor Author

@tgross35 Thanks for your review! It seems an ACP is an issue at https://github.com/rust-lang/libs-team/issues/new/choose.

If so, let me try to find some time and write a formal proposal.

@bors
Copy link
Contributor

bors commented Oct 15, 2023

☔ The latest upstream changes (presumably #116742) made this pull request unmergeable. Please resolve the merge conflicts.

@Dylan-DPC
Copy link
Member

@tisonkun any updates on the ACP?

@dtolnay dtolnay assigned dtolnay and unassigned m-ou-se Nov 5, 2023
@dtolnay
Copy link
Member

dtolnay commented Nov 5, 2023

These methods are the equivalent of the lazycell crate's borrow_mut_with and try_borrow_mut_with, which are used by Cargo, blocking rust-lang/cargo#9310. So I am eager for this to be supported better by OnceCell.

@dtolnay dtolnay added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 5, 2023
@tisonkun
Copy link
Contributor Author

tisonkun commented Nov 6, 2023

Thanks for your reply! Let me file an ACP in this week :D

@tisonkun
Copy link
Contributor Author

Breifly outline it here - rust-lang/libs-team#294

Looking forward to your comments and what's the next step I should do.

Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please rebase this, and get it to compile?

#114788 (comment) shows the compile error.

@Dylan-DPC Dylan-DPC added S-waiting-on-ACP Status: PR has an ACP and is waiting for the ACP to complete. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 22, 2024
@tisonkun
Copy link
Contributor Author

Could you please rebase this, and get it to compile?

OK. Let me schedule it in weeks.

@programmerjake

This comment was marked as resolved.

@tisonkun
Copy link
Contributor Author

@programmerjake would you possibly drop an approval or pull maintainers that can make the decision? I've tried to ask on Zulip.

@tgross35
Copy link
Contributor

Please squash this when you get the chance (still looks ok to me)

@programmerjake
Copy link
Member

@programmerjake would you possibly drop an approval or pull maintainers that can make the decision? I've tried to ask on Zulip.

sorry, i'm not one of those who can make a decision here, @dtolnay is assigned, if they haven't responded in a month or so you might be able to use some rustbot command to pick a different assignee, though that could also make it take longer since you'd likely be at the back of that new person's queue.

@tisonkun
Copy link
Contributor Author

@tgross35 squashed.

@programmerjake Thank you.

Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thank you!

@dtolnay
Copy link
Member

dtolnay commented Apr 5, 2024

@bors r+

@bors
Copy link
Contributor

bors commented Apr 5, 2024

📌 Commit 95e195f has been approved by dtolnay

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 5, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Apr 6, 2024
impl get_mut_or_init and get_mut_or_try_init for OnceCell and OnceLock

See also rust-lang#74465 (comment)

I'm trying to understand the process for such proposal. And I'll appreciate it if anyone can guide me the next step for consensus or adding tests.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Apr 6, 2024
impl get_mut_or_init and get_mut_or_try_init for OnceCell and OnceLock

See also rust-lang#74465 (comment)

I'm trying to understand the process for such proposal. And I'll appreciate it if anyone can guide me the next step for consensus or adding tests.
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 6, 2024
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#114788 (impl get_mut_or_init and get_mut_or_try_init for OnceCell and OnceLock)
 - rust-lang#122291 (Stabilize `const_caller_location` and `const_location_fields`)
 - rust-lang#123321 (Bump dependencies)
 - rust-lang#123339 (optimize tidy check on `src/tools/tidy/src/issues.txt`)
 - rust-lang#123357 (CI: Redirect stderr to stdout to order GHA logs)
 - rust-lang#123504 (bootstrap: split cargo-miri test into separate Step)

r? `@ghost`
`@rustbot` modify labels: rollup
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Apr 6, 2024
impl get_mut_or_init and get_mut_or_try_init for OnceCell and OnceLock

See also rust-lang#74465 (comment)

I'm trying to understand the process for such proposal. And I'll appreciate it if anyone can guide me the next step for consensus or adding tests.
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 6, 2024
…iaskrgr

Rollup of 5 pull requests

Successful merges:

 - rust-lang#114788 (impl get_mut_or_init and get_mut_or_try_init for OnceCell and OnceLock)
 - rust-lang#122291 (Stabilize `const_caller_location` and `const_location_fields`)
 - rust-lang#123339 (optimize tidy check on `src/tools/tidy/src/issues.txt`)
 - rust-lang#123357 (CI: Redirect stderr to stdout to order GHA logs)
 - rust-lang#123504 (bootstrap: split cargo-miri test into separate Step)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 6, 2024
…iaskrgr

Rollup of 4 pull requests

Successful merges:

 - rust-lang#114788 (impl get_mut_or_init and get_mut_or_try_init for OnceCell and OnceLock)
 - rust-lang#122291 (Stabilize `const_caller_location` and `const_location_fields`)
 - rust-lang#123357 (CI: Redirect stderr to stdout to order GHA logs)
 - rust-lang#123504 (bootstrap: split cargo-miri test into separate Step)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 3bcf402 into rust-lang:master Apr 6, 2024
11 checks passed
@rustbot rustbot added this to the 1.79.0 milestone Apr 6, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Apr 6, 2024
Rollup merge of rust-lang#114788 - tisonkun:get_mut_or_init, r=dtolnay

impl get_mut_or_init and get_mut_or_try_init for OnceCell and OnceLock

See also rust-lang#74465 (comment)

I'm trying to understand the process for such proposal. And I'll appreciate it if anyone can guide me the next step for consensus or adding tests.
@bors
Copy link
Contributor

bors commented Apr 6, 2024

⌛ Testing commit 95e195f with merge 83d0a94...

@tisonkun tisonkun deleted the get_mut_or_init branch April 6, 2024 15:09
@tisonkun
Copy link
Contributor Author

tisonkun commented Apr 6, 2024

Thanks for your review and merge @dtolnay! Two questions here:

  1. How do I keep track on the following stabilize process? Is there anything that should be done by me?
  2. I'd like to propose a with_extension API for PathBuf. Then I should go through a similar process to this one? (ACP, and PR)

@dtolnay
Copy link
Member

dtolnay commented Apr 6, 2024

  1. After some time, a library team member will propose this for stabilization in the tracking issue (Tracking Issue for OnceCell/Lock::get_mut_or_init #121641). You should pay attention on that issue and address any design discussion that arises as people try this API in nightly.

  2. That's right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-ACP Status: PR has an ACP and is waiting for the ACP to complete. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants