Open
Listed in
Description
This supercedes #74465 after a portion of once_cell
was stabilized with #105587
Feature gate: #![feature(once_cell_try)]
This is a tracking issue for the *try*
methods that go with the once_cell
feature. Initially they were part of stabilization under once_cell
(#105587) but was removed due to compatibility concerns with try trait v2 (#105587 (comment)).
Public API
impl<T> OnceCell<T> {
pub fn get_or_try_init<F, E>(&self, f: F) -> Result<&T, E> where F: FnOnce() -> Result<T, E>;
}
impl<T> OnceLock<T> {
pub fn get_or_try_init<F, E>(&self, f: F) -> Result<&T, E> where F: FnOnce() -> Result<T, E>;
}
Steps / History
- Original implementation: Add lazy initialization primitives to std #72414Final comment period (FCP)1Stabilization PR
Unresolved Questions
- None yet.
cc @joboet @matklad just for reference
Footnotes
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
once_cell
#105587once_cell
#74465BurntSushi commentedon Mar 30, 2023
I think I'd say that the question of whether to make the fallible routines generic over
Try
is unresolved at this point. See: #107122 (comment) and #84277 (comment)tgross35 commentedon Mar 30, 2023
I'm kind of curious of how and how often these functions are used in the ecosystem, does anyone know of any examples? They're not used anywhehre in
rustc
BurntSushi commentedon Mar 30, 2023
I posted a comment on that PR with the results of a quick search that I did: #107122 (comment)
lazy_static
withonce_cell
hickory-dns/hickory-dns#1944lazy_static
withonce_cell
sfackler/rust-native-tls#267v1.0.0
netizen-a/dylink#21sutes-work commentedon Feb 22, 2024
Related to this, I want something like: TryLazyLock. Not sure if this is the right place to comment (sorry if it's not!).
I looked to see how get_or_try_init is implemented, but unfortunately, the
poison
method on Once isn't exposed publicly. It would be nice of Once had an API that would support this.tgross35 commentedon Feb 22, 2024
@sutes-work can you give an example of what a TryLazyLock API would do differently from
get_or_try_init
?sutes-work commentedon Feb 22, 2024
So I can do this:
and it's not so bad, but it costs me space for the init_fn. I should be able to share that space with T. I should be able to have:
It's a bit rough, but hopefully you get the idea.
32 remaining items