-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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 OnceCell/Lock::try_insert()
#116693
Comments
…, r=Mark-Simulacrum Implement `OnceCell/Lock::try_insert()` I took inspiration from [`once_cell`](https://crates.io/crates/once_cell): - [`once_cell::unsync::OnceCell::try_insert()`](https://github.com/matklad/once_cell/blob/874f9373abd7feaf923a3b3c34bfb3383529c671/src/lib.rs#L551-L563) - [`once_cell::sync::OnceCell::try_insert()`](https://github.com/matklad/once_cell/blob/874f9373abd7feaf923a3b3c34bfb3383529c671/src/lib.rs#L1080-L1087) I tried to change as little code as possible in the first commit and applied some obvious optimizations in the second one. ACP: rust-lang/libs-team#276 Tracking issue: rust-lang#116693
Rollup merge of rust-lang#116540 - daxpedda:once-cell-lock-try-insert, r=Mark-Simulacrum Implement `OnceCell/Lock::try_insert()` I took inspiration from [`once_cell`](https://crates.io/crates/once_cell): - [`once_cell::unsync::OnceCell::try_insert()`](https://github.com/matklad/once_cell/blob/874f9373abd7feaf923a3b3c34bfb3383529c671/src/lib.rs#L551-L563) - [`once_cell::sync::OnceCell::try_insert()`](https://github.com/matklad/once_cell/blob/874f9373abd7feaf923a3b3c34bfb3383529c671/src/lib.rs#L1080-L1087) I tried to change as little code as possible in the first commit and applied some obvious optimizations in the second one. ACP: rust-lang/libs-team#276 Tracking issue: rust-lang#116693
Should the return type be |
I'm working on #121641 and wonder if we should add |
Is there a reason, why this takes a value directly instead of an I would like to use this in a case where I have to know after a |
I'm unsure how I missed these comments but here goes:
Given that all these options fulfill the requirements, personally I'm still in favor of
I'm assuming you mean adding new The only use case I know of is only valid if the value is shared, otherwise
That sounds quite reasonable to me, I will make a PR proposing this change when I get to it. |
While implementing it I realized that this is a bit different. This API is more similar to let mut value = Some(value);
match self.try_insert(|| value.take().unwrap()) {
Ok(value) => do_your_thing(),
Err(old_value) => {
let new_value = value.unwrap();
do_your_other_thing();
}
} So I think this would need another API addition. |
Feature gate:
#![feature(once_cell_try_insert)]
This is a tracking issue for
OnceCell::try_insert()
andOnceLock::try_insert()
.This adds a method similarly to
OnceCell/Lock::set()
but returns a reference at the same time. This is also similar toOnceCell/Lock::get_or_init()
but the return value also tells you if the value was actually inserted or if theOnceCell/Lock
was already occupied.Public API
Steps / History
OnceCell/Lock::try_insert()
libs-team#276OnceCell/Lock::try_insert()
#116540Unresolved Questions
Footnotes
https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html ↩
The text was updated successfully, but these errors were encountered: