0

I want to store pointers to async functions in some static list and call them later. How can I do this? E.g.

//async
fn foo() {
}

//async
fn bar() {
}

type MyFn = fn();

const RUNNERS: &[MyFn] = &[
    foo,
    bar,
];

async fn run() {
    RUNNERS[0]()
    //RUNNERS[0]().await
}

fn main() {
    let _ = run();
}

is a working version for non-async functions.

How have I to implement this so that I can uncomment the async and await like in this playground to a non-working version?

ensc
  • 6,704
  • 14
  • 22
  • Have a look at https://stackoverflow.com/questions/58173711/how-can-i-store-an-async-function-in-a-struct-and-call-it-from-a-struct-instance and https://stackoverflow.com/questions/27994509/how-do-i-call-a-function-through-a-member-variable – Xander Bielby Jul 09 '21 at 09:59
  • @AlexanderBielby this does not really seems to apply; it is solved there by generics or boxes which does not work for the static `const RUNNER` – ensc Jul 09 '21 at 10:20
  • @ensc it is not currently possible, but you can use [lazy_static](https://crates.io/crates/lazy_static) instead for similar results. – Locke Jul 09 '21 at 11:18

0 Answers0