tokio icon indicating copy to clipboard operation
tokio copied to clipboard

Add an async Once type.

Open VixieTSQ opened this issue 3 years ago • 2 comments

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

VixieTSQ avatar May 12 '22 18:05 VixieTSQ

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.

Darksonn avatar May 12 '22 20:05 Darksonn

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.

That's another temporary solution yep! Still feels much more like a hack than a real solution, though. OP updated with advice regarding AtomicBool.

VixieTSQ avatar May 12 '22 23:05 VixieTSQ