-
I worte following code for a test: //error[E0391]: cycle detected when computing type of `tokio_recure::{opaque#0}`
async fn tokio_recure(cnt: i32) {
if cnt > 0 {
let j = tokio::task::spawn(async move { tokio_recure(cnt - 1).await });
j.await.unwrap()
}
}
// it's ok
async fn tokio_local_recure(cnt: i32) {
if cnt > 0 {
let j = tokio::task::spawn_local(async move { tokio_local_recure(cnt - 1).await });
j.await.unwrap()
}
} I'm not sure if this acts as expected. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
Usually, recursive blowups like this can be solved by boxing the future. For the recursive task, try boxing and casting it to |
Beta Was this translation helpful? Give feedback.
-
This is definitely a bit unexpected. |
Beta Was this translation helpful? Give feedback.
-
I don't know why it only happens for one of them, but the easiest way to solve this is to wrap the fn spawn_tokio_recure(cnt: i32) -> JoinHandle<()> {
tokio::task::spawn(tokio_recure(cnr))
}
async fn tokio_recure(cnt: i32) {
if cnt > 0 {
let j = spawn_tokio_recure(cnt - 1);
j.await.unwrap()
}
} |
Beta Was this translation helpful? Give feedback.
I don't know why it only happens for one of them, but the easiest way to solve this is to wrap the
tokio::spawn
call in a non-async function.