Hello everyone,
I recently discovered the linkme crate, and I find its capabilities to be incredibly compelling.
The ability to statically distribute code at virtually no cost, and with a convenience that rivals dynamic dispatch, is an excellent feature. I have successfully used it to build several small utilities, and I am now hoping to apply it to a more substantial project.
However, I have encountered an unexpected challenge. I had assumed that distributing an async fn would be as straightforward as a regular fn. After attempting various approaches, I have come to realise this is not the case. It appears that distributing an async fn necessitates incurring the overhead associated with dyn, a limitation I find quite frustrating.
I would be very grateful for any suggestions from the community on this matter. Is there a known method to statically dispatch async fn with the same convenience that linkme provides for synchronous functions, but without resorting to the dyn keyword?
Static dispatch requires the compiler know everything that is being dispatched to. Unless the compiler were to gain some kind of built-in feature for collecting things, anything that provides the functionality linkme does, if you use it to call functions, will necessarily involve dynamic dispatch to those functions. (This is not unique to async. fn pointers are dynamic dispatch too!)
Also, it is likely that you are over-estimating the costs of dynamic dispatch.
Note that calling a fn() also involves dynamic dispatch. I will give you that returning a trait object will likely involve an allocation too, and hence be a bit more costly than just performing dynamic dispatch, but it that really that costly in your context? You're executing an async fn, so you most likely also have an executor that internally will box the futures anyway. And it's also pretty likely that many of the "end" async operations will be more costly than a one-time allocations. In other words, are you sure you're not optimizing prematurely?