Skip to content

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
Discussion options

You must be logged in to vote

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 || and the async block, in that order. (If you try to do async || { ... }, you get an error about unstable features. Just swap them and it will work.)

Replies: 1 comment 6 replies

Comment options

You must be logged in to vote
6 replies
@Darksonn
Comment options

@xiashaohua
Comment options

@xiashaohua
Comment options

@xiashaohua
Comment options

@Darksonn
Comment options

Answer selected by xiashaohua
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants