Add an async Once type.
Is your feature request related to a problem? Please describe. I would like to be able to use async code inside a Once.
Describe the solution you'd like
A solution similar to how get_or_init() on tokio OnceCell takes a closure should work (I don't know much about this but I saw that it exists.)
Describe alternatives you've considered A current work around would be to use an atomic bool something like this:
static DB_EXISTS: AtomicBool = AtomicBool::new(false);
// snip
let exists = INIT_DONE.swap(true, Ordering::SeqCst);
if exists {panic!("Initialization already done! This is a bug.");} // or handle it however you like
// Do initialization here
You can use an OnceCell with item type (). If you want to use an AtomicBool, then you should use swap rather than a load followed by a store.
You can use an
OnceCellwith item type(). If you want to use anAtomicBool, then you should useswaprather than aloadfollowed by astore.
That's another temporary solution yep! Still feels much more like a hack than a real solution, though. OP updated with advice regarding AtomicBool.