dtolnay / async-trait

Type erasure for async trait methods
Apache License 2.0
1.81k stars 84 forks source link

Help: how to work with catch_unwind. #176

Closed jjeffcaii closed 3 years ago

jjeffcaii commented 3 years ago

Hi:

I got a compile error when using catch_unwind, I don't know how to change my code to resolve this problem.

Here's a simple example:

use async_trait::async_trait;
use futures::FutureExt;

#[async_trait]
pub trait Foobar: Sync + Send + 'static {
    async fn foobar(&self);
}

pub struct FoobarImpl;

#[async_trait]
impl Foobar for FoobarImpl {
    async fn foobar(&self) {
        println!("foobar");
    }
}

async fn test(x: FoobarImpl) {
    x.foobar().catch_unwind().await; // <- cannot compile
}

Thanks!

taiki-e commented 3 years ago

the future returned by foobar is not UnwindSafe. you need to use AssertUnwindSafe

-     x.foobar().catch_unwind().await;
+     AssertUnwindSafe(x.foobar()).catch_unwind().await;

playground

jjeffcaii commented 3 years ago

@taiki-e It works! Thanks! 😄