matklad / once_cell

Rust library for single assignment cells and lazy statics without macros
Apache License 2.0
1.87k stars 109 forks source link

Add `OnceCell::set_and_get` for async init #134

Closed tamuhey closed 3 years ago

tamuhey commented 3 years ago

Related: #108

I want to initialize OnceCell with async function, but as #108, it seems difficult to support it right now. To address this issue, I wrote a function like below:

async fn get_foo() -> Foo {
    if let Some(foo) = Foo.get() {
        return foo;
    }
    let foo = ...
    Foo.get_or_init(|| foo)  <- I want to execute this line without creating a closure
}

I want a method like set_and_get to avoid to use the closure in last line.

matklad commented 3 years ago

see https://github.com/matklad/once_cell/issues/108#issuecomment-762072263