Using async-trait macro to have sync function inside a trait

#![allow(unused)]

use async_trait::async_trait;

#[derive(Debug)]
struct Foo<'a> {
    a: Option<&'a i32>,
    b: Option<&'a mut i32>,
    c: Option<i32>,
}

#[async_trait]
trait Trait {
    async fn fun(&self, arg: Foo) -> Result<i32, i32>;
}
fn main() {
    let a = 100;
    let mut b = 200;
    let c = 300;
    println!("{:?}", b);
    let foo = Foo {
        a: Some(&a),
        b: Some(&mut b),
        c: Some(c),
    };
    fun(foo);
    println!("{:?}", b);

    let foo = Foo {
        a: Some(&a),
        b: Some(&mut b),
        c: Some(c),
    };
    let br = Bar {};
    br.fun(foo);
    println!("{:?}", b);
}
struct Bar {}

#[async_trait]
impl Trait for Bar {
    async fn fun(&self, arg: Foo) -> Result<i32, i32> {
        let a = arg.a.expect("a not set");
        let b = arg.b.expect("b not set");
        let c = arg.c.expect("c not set");
        *b = 278;
        Ok(100)
    }
}

fn fun(arg: Foo) -> Result<i32, i32> {
    let a = arg.a.expect("a not set");
    let b = arg.b.expect("b not set");
    let c = arg.c.expect("c not set");
    *b = 2;

    Ok(100)
}

In the above code I have two fun() variants.one is a basic fun() and other is bind to a async-trait . basic function works as expected but same code failing with below error when I use it under async-trait. I'm not much aware of async & await in rust ,so kindly need help in resolving this error.

error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
  --> src/main.rs:42:55
   |
42 |       async fn fun(&self, arg: Foo) -> Result<i32, i32> {
   |  _______________________________________________________^
43 | |         let a = arg.a.expect("a not set");
44 | |         let b = arg.b.expect("b not set");
45 | |         let c = arg.c.expect("c not set");
46 | |         *b = 278;
47 | |         Ok(100)
48 | |     }
   | |_____^
   |
note: first, the lifetime cannot outlive the anonymous lifetime defined here...
  --> src/main.rs:42:30
   |
42 |     async fn fun(&self, arg: Foo) -> Result<i32, i32> {
   |                              ^^^
note: ...so that the types are compatible
  --> src/main.rs:42:55
   |
42 |       async fn fun(&self, arg: Foo) -> Result<i32, i32> {
   |  _______________________________________________________^
43 | |         let a = arg.a.expect("a not set");
44 | |         let b = arg.b.expect("b not set");
45 | |         let c = arg.c.expect("c not set");
46 | |         *b = 278;
47 | |         Ok(100)
48 | |     }
   | |_____^
   = note: expected `(&Bar, Foo<'_>)`
              found `(&'life0 Bar, Foo<'_>)`
note: but, the lifetime must be valid for the lifetime `'async_trait` as defined here...
  --> src/main.rs:42:14
   |
42 |     async fn fun(&self, arg: Foo) -> Result<i32, i32> {
   |              ^^^^^^^^^^^^^^^^^^^^
note: ...so that the types are compatible
  --> src/main.rs:42:55
   |
42 |       async fn fun(&self, arg: Foo) -> Result<i32, i32> {
   |  _______________________________________________________^
43 | |         let a = arg.a.expect("a not set");
44 | |         let b = arg.b.expect("b not set");
45 | |         let c = arg.c.expect("c not set");
46 | |         *b = 278;
47 | |         Ok(100)
48 | |     }
   | |_____^
   = note: expected `Pin<Box<(dyn Future<Output = Result<i32, i32>> + Send + 'async_trait)>>`
              found `Pin<Box<dyn Future<Output = Result<i32, i32>> + Send>>`

For more information about this error, try `rustc --explain E0495`.

It's explained here in its docs.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.