0

impl get_mut_or_init and get_mut_or_try_init for OnceCell and OnceLock by tisonk...

 1 week ago
source link: https://github.com/rust-lang/rust/pull/114788
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

impl get_mut_or_init and get_mut_or_try_init for OnceCell and OnceLock #114788

Conversation

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.

Collaborator

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

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

This comment has been minimized.

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).

eggyal reacted with thumbs up emoji

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

Member

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

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

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()
    }

Contributor

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...)

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

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.

Contributor

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

Member

@tisonkun any updates on the ACP?

Member

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

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

Contributor

Author

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

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.

Member

@dtolnay dtolnay

left a comment

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

#114788 (comment) shows the compile error.

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

Contributor

Author

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

OK. Let me schedule it in weeks.

This comment was marked as resolved.

Contributor

Author

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

Contributor

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

@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.

Contributor

Author

Member

@dtolnay dtolnay

left a comment

Looks good. Thank you!

Member

@bors r+

Contributor

📌 Commit 95e195f has been approved by dtolnay

It is now in the queue for this repository.

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

bors

merged commit 3bcf402 into

rust-lang:master

Apr 6, 2024

11 checks passed

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

Contributor

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

tisonkun

deleted the get_mut_or_init branch

April 6, 2024 15:09

Contributor

Author

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)

Member

  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.

tisonkun reacted with thumbs up emoji

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

programmerjake

programmerjake left review comments

dtolnay

dtolnay approved these changes

tgross35

tgross35 approved these changes
Assignees

dtolnay

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

Milestone

1.79.0

Development

Successfully merging this pull request may close these issues.

None yet

10 participants

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK