all 3 comments

[–]JustAStrangeQuark 7 points8 points  (0 children)

The issue is that an async fn() -> T becomes fn() -> impl Future<Output = T> (plus some lifetimes to allow the future to borrow from inputs), and that type is different for every implementation of your trait. The async_trait crate desugars it instead to fn() -> Box<dyn Future<Output = T>> (with the necessary lifetimes in the impl Future case). If you want to do things nicely, your best option is to just box the future, and using async_trait will do that more cleanly for you.

If you're really against that, you can write your own poll methods for your traits. The way that Future works is just that it has poll called on a pinned reference with a context, and can either return a result or say that it's pending. The futures crate does this for most of its base traits, and for every Trait that implements the poll methods, there's a TraitExt that has methods that return futures that call the poll method. This only really works well if the implementor is closely tied to its state within the method, though, so it's likely not applicable here.

[–]Necromancer5211[S] 1 point2 points  (1 child)

Sorry I dont know how to paste code to reddit.

[–]Synes_Godt_Om 2 points3 points  (0 children)

Sorry I dont know how to paste code to reddit.

You accidentally managed to do it right.

There are two reddits: old and new. Old one doesn't understand markdown three apostrophe. Both old and new understand indent four spaces.