im making a library and theres a method that when called should do something after a duration in the background, this is what i came up with
pub async fn exec_temp(client: &Client, duration: Duration) -> Result<Resp> {
let resp = exec(client).await?;
let resp_id: u64 = resp.id;
tokio::spawn(async move {
tokio::time::sleep(duration).await;
let _ = destroy(client, resp_id).await;
});
Ok(resp)
}
but theres a few issues with this
- the result returned later cant be handled
- the background task cant be cancelled
but i have a bigger problem, if i have a reference to the client, i cant use that to spawn a task (apparently?) i could take an Arc<Client>
but in the real world users will have an Arc<Context>
which wraps over that client so yeah idk what to do now
so what do you think is a better approach for this? thank you :)