I want to get a pointer to the async run function, at runtime. In practice I'll have let generic_async_runner = Box<dyn GenericAsyncRunner> and I want generic_async_runner.return_run_function().await. I could downcast but in practice there will be lots of different SomethingAsyncRunner so downcasting would have to try to downcast to all of them.
Can I rewrite async fn run() -> Result<u32, AsyncRunError> into a desugared version that returns a Future? In this way I can have a concrete type to put in ? on the code above.
/* some details omitted */
pub trait GenericAsyncRunner{
type Run: Future<Output=Result<u32, AsyncRunError>>;
fn return_run_function() -> fn() -> Self::Run;
}
// This TAIT is effectively giving a name to your opaque type
type SpecificAsyncType = impl Future<Output=Result<u32, AsyncRunError>>;
impl SpecificAsyncRunner {
// The TAIT is defined by this implementation. We can't use the `async fn`
// sugar, or it interferes with the "defining use". So instead we use a
// normal function containing an `async` block.
fn run() -> SpecificAsyncType {
async { todo!() }
}
}
impl GenericAsyncRunner for SpecificAsyncRunner {
// Now we can name the opaque type
type Run = SpecificAsyncType;
fn return_run_function() -> fn() -> Self::Run {
Self::run
}
}