I was experimenting with the async_trait crate and stumble upon the following error:
future created by async block is not Send
by the following code:
#[async_trait::async_trait]
trait DatabaseManagerGetConnection {
async fn get_connection(&self) -> Option<()>;
}
#[async_trait::async_trait]
trait EntityRepositoryExecute {
async fn execute (&self) -> Option<()>;
}
trait TableMapperToEntity {
fn to_entity(&self) -> Option<()>;
}
struct DatabaseManager {
sqlx_db_pool: sqlx::Pool<sqlx::Postgres>
}
#[async_trait::async_trait]
impl DatabaseManagerGetConnection for DatabaseManager {
async fn get_connection(&self) -> Option<()> {
Some(())
}
}
struct EntityRepository <D: DatabaseManagerGetConnection>{
database_manager: std::sync::Arc<D>
}
#[async_trait::async_trait]
// impl <D: DatabaseManagerGetConnection + Send + Sync> EntityRepositoryExecute for EntityRepository<D> {
impl <D: DatabaseManagerGetConnection> EntityRepositoryExecute for EntityRepository<D> {
async fn execute(&self) -> Option<()>{
self.database_manager.get_connection().await.unwrap();
Some(())
}
}
fn main() {
}
After a couple hours trying to understand the issue I tried to swap Line 32
impl <D: DatabaseManagerGetConnection> EntityRepositoryExecute for EntityRepository<D> {
with line Line 31:
impl <D: DatabaseManagerGetConnection + Send + Sync> EntityRepositoryExecute for EntityRepository<D> {
and the error went way. But I still confused on:
why I would need to add Send and Sync trait to the D: DatabaseManagerGetConnection?
My understanding was that async_trait::async_trait macro was already adding these "guarantees" (traits) to the DatabaseManagerGetConnection trait return itself.
As far as I am aware:
Arc implements Send + Sync
and sqlx::Pool<sqlx::Postgres> also implements Send + Sync
What am I missing?
4 posts - 3 participants
Read full topic
| # | Наименование новости | Тональность | Информативность | Дата публикации |
|---|---|---|---|---|
| 1 | Trait object with associated types in field - requires generic param? | 0 | 11.08 | 07-10-2026 |
| 2 | Are there wait-free priority queue crates? | 0 | 16.09 | 05-10-2026 |
| 3 | Strategies for dyn trait objects with optional behavior? | 0 | 7.12 | 06-10-2026 |
| 4 | Toasty, an async ORM for Rust, is now on crates.io | 0 | 10 | 03-04-2026 |
| 5 | Does my lockfree stack impl look correct? | 0 | 6.06 | 05-10-2026 |
| 6 | Architecture review of a Hyper/Tower-based web framework (Toxi) | 0 | 15.46 | 06-10-2026 |
| 7 | Why no Dependent Matrix Type? | 0 | 5.16 | 08-10-2026 |
| 8 | Feedback wanted: compio-pool, a thread-per-core connection pool for !Send connections | 0 | 17.89 | 08-10-2026 |
| 9 | Задача взяла lock() и остановила весь рантайм: пять ошибок с блокировками в async Rust | 0 | 6.57 | 07-09-2026 |