Does tokio have a trait like 'Fn' that passes an asynchronous closure function as an argument #3700
Answered
by
Darksonn
xiashaohua
asked this question in
Q&A
-
I want to use a task executor to execute cycletasks. It is fine to execute synchronous functions, but passing in asynchronous functions will give an error。the error is :
Then i add the Future,The error report is the same as before:
|
Beta Was this translation helpful? Give feedback.
Answered by
Darksonn
Apr 13, 2021
Replies: 1 comment 6 replies
-
The trait bound you are looking for is probably something like: fn register_task<F, Fut>(..., func: F)
where
F: Fn() -> Fut,
Fut: Future<Output = ()>, You would then be able to call it with tm.register_task(CRON, "1/5 * * * * *".to_string(), "test3".to_string(), || async {test2().await;}); or even tm.register_task(CRON, "1/5 * * * * *".to_string(), "test3".to_string(), || test2()); Note that you need both the |
Beta Was this translation helpful? Give feedback.
6 replies
Answer selected by
xiashaohua
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The trait bound you are looking for is probably something like:
You would then be able to call it with
or even
Note that you need both the
||
and the async block, in that order. (If you try to doasync || { ... }
, you get an error about unstable features. Just swap them and it will work.)