Вход на сайт

Просмотр новости

Найдите то, что Вас интересует

Crate async_trait - error future created by async block is not Send

Дата публикации: 05-10-2026 00:12:59


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

Схожие новости

#Наименование новостиТональностьИнформативностьДата публикации
1Trait object with associated types in field - requires generic param?011.0807-10-2026
2Are there wait-free priority queue crates?016.0905-10-2026
3Strategies for dyn trait objects with optional behavior?07.1206-10-2026
4Toasty, an async ORM for Rust, is now on crates.io01003-04-2026
5Does my lockfree stack impl look correct?06.0605-10-2026
6Architecture review of a Hyper/Tower-based web framework (Toxi)015.4606-10-2026
7Why no Dependent Matrix Type?05.1608-10-2026
8Feedback wanted: compio-pool, a thread-per-core connection pool for !Send connections017.8908-10-2026
9Задача взяла lock() и остановила весь рантайм: пять ошибок с блокировками в async Rust06.5707-09-2026

Классификация: . Схожих патентов: 0. Схожих новостей: 9. Тональность: 0. Информативность: 5.4. Источник: users.rust-lang.org.