Compilation error which says future created by async block is not `Send`

I have recently started with Rust and currently I don't have much knowledge on async,await,future in Rust. My code looks similar to below one and I'm stuck with an error which says future created by async block is not Send . I don't understand what is this error about and how to resolve it. Any help in resolving it?

#![allow(unused)]
use async_trait::async_trait;
use futures::prelude::*;
use std::error::Error;

//Assume these 2 lines are part  module-1 to which currently I dont have the right to edit the code.
type JoinFuture = std::pin::Pin<Box<dyn Future<Output = Result<(), Box<dyn Error + Send + Sync>>>>>;
pub struct Logger{
    var:Vec<JoinFuture>,
}

//below lines are part of module-2 in which I'm currently working on.
struct A{
logger : Logger,

}

#[async_trait]
trait Interface {
    
    async fn setup(&mut self) ;
}

#[async_trait]
impl Interface for A {
    //compilation error is pointing to below line
    async fn setup(&mut self) {}
}


fn main() {

}

error: future cannot be sent between threads safely
  --> src/main.rs:26:31
   |
26 |     async fn setup(&mut self) {}
   |                               ^^ future created by async block is not `Send`
   |
   = help: the trait `std::marker::Send` is not implemented for `(dyn futures::Future<Output = Result<(), Box<(dyn std::error::Error + Sync + std::marker::Send + 'static)>>> + 'static)`
note: captured value is not `Send` because `&` references cannot be sent unless their referent is `Sync`
  --> src/main.rs:26:25
   |
26 |     async fn setup(&mut self) {}
   |                         ^^^^ has type `&mut A` which is not `Send`, because `A` is not `Sync`
   = note: required for the cast to the object type `dyn futures::Future<Output = ()> + std::marker::Send`

error: could not compile `_test` due to previous error

It's because the future in your dyn Future isn't marked with + Send. (only the error type is)

I recommend that you use the BoxFuture alias from the futures crate, which handles that for you.

1 Like

Thanks! this solved my issue

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.