From 5a7740729738ec346e41e4daee87aa01d027e626 Mon Sep 17 00:00:00 2001 From: Martin Berg Alstad Date: Fri, 7 Mar 2025 22:40:10 +0100 Subject: [PATCH] :sparkles: Replaced async_trait with async in traits from 2024 edition --- Cargo.lock | 3 -- Cargo.toml | 2 -- crates/diesel_crud_derive/src/common.rs | 2 +- crates/diesel_crud_derive/src/create.rs | 47 +++++++++---------------- crates/diesel_crud_derive/src/delete.rs | 32 +++++++---------- crates/diesel_crud_derive/src/list.rs | 14 +++----- crates/diesel_crud_derive/src/read.rs | 24 +++++-------- crates/diesel_crud_derive/src/update.rs | 24 +++++-------- crates/diesel_crud_trait/Cargo.toml | 2 -- crates/diesel_crud_trait/src/lib.rs | 32 ++++++++++------- src/axum/response.rs | 6 ++-- src/axum/router.rs | 6 ++-- src/diesel/get_connection.rs | 7 ++-- src/serde/traits.rs | 6 ++-- src/test/diesel_pool.rs | 2 -- src/test/test_containers.rs | 8 ++++- 16 files changed, 90 insertions(+), 127 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index de747f2..8ca0322 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -488,8 +488,6 @@ dependencies = [ name = "diesel-crud-trait" version = "0.1.0" dependencies = [ - "async-trait", - "deadpool-diesel", "diesel", "diesel-async", "thiserror 2.0.12", @@ -1072,7 +1070,6 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" name = "lib" version = "1.5.0" dependencies = [ - "async-trait", "axum", "chrono", "deadpool-diesel", diff --git a/Cargo.toml b/Cargo.toml index 1611d7e..bd17fb5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,6 @@ tower = { version = "0.5", optional = true } tower-http = { version = "0.6", optional = true, features = ["trace", "cors", "normalize-path"] } mime = { version = "0.3", optional = true } # Async -async-trait = { workspace = true } tokio = { workspace = true, optional = true, features = ["fs", "rt-multi-thread"] } tokio-util = { version = "0.7", optional = true, features = ["io"] } # Database @@ -59,7 +58,6 @@ derive_more = { workspace = true, features = ["from", "constructor"] } [workspace.dependencies] # Async tokio = "1.40" -async-trait = "0.1" # Database diesel = "2.2" diesel-async = "0.5" diff --git a/crates/diesel_crud_derive/src/common.rs b/crates/diesel_crud_derive/src/common.rs index 52868a6..c5ba984 100644 --- a/crates/diesel_crud_derive/src/common.rs +++ b/crates/diesel_crud_derive/src/common.rs @@ -9,6 +9,6 @@ pub(crate) struct PrimaryKey { pub(crate) fn return_type(output: proc_macro2::TokenStream) -> proc_macro2::TokenStream { quote! { - std::pin::Pin> + Send + 'async_trait>> + Result<#output, lib::diesel_crud_trait::CrudError> } } diff --git a/crates/diesel_crud_derive/src/create.rs b/crates/diesel_crud_derive/src/create.rs index efafe3c..ef3b840 100644 --- a/crates/diesel_crud_derive/src/create.rs +++ b/crates/diesel_crud_derive/src/create.rs @@ -1,4 +1,4 @@ -use crate::{common, Attributes}; +use crate::{Attributes, common}; use quote::quote; pub(crate) fn derive_diesel_crud_create_impl( @@ -16,37 +16,24 @@ pub(crate) fn derive_diesel_crud_create_impl( #[automatically_derived] impl lib::diesel_crud_trait::DieselCrudCreate<#table::table> for #struct_ident { type Insert = #insert; - fn insert<'a, 'async_trait>(insert: Self::Insert, conn: &'a mut diesel_async::AsyncPgConnection) -> #return_type - where - Self: Sized + Sync + 'a, - 'a: 'async_trait, - { - Box::pin(async move { - use diesel::associations::HasTable; - diesel_async::RunQueryDsl::get_result( - diesel::dsl::insert_into(#table::table::table()).values(insert), - conn - ) - .await - .map_err(Into::into) - }) + async fn insert(insert: Self::Insert, conn: &mut diesel_async::AsyncPgConnection) -> #return_type { + use diesel::associations::HasTable; + diesel_async::RunQueryDsl::get_result( + diesel::dsl::insert_into(#table::table::table()).values(insert), + conn + ) + .await + .map_err(Into::into) } - fn insert_many<'a, 'b, 'async_trait>(insert: &'a [Self::Insert], conn: &'b mut diesel_async::AsyncPgConnection) -> #many_return_type - where - Self: Sized + Sync + 'async_trait, - 'a: 'async_trait, - 'b: 'async_trait, - { - Box::pin(async move { - use diesel::associations::HasTable; - diesel_async::RunQueryDsl::get_results( - diesel::dsl::insert_into(#table::table::table()).values(insert), - conn - ) - .await - .map_err(Into::into) - }) + async fn insert_many(insert: &[Self::Insert], conn: &mut diesel_async::AsyncPgConnection) -> #many_return_type { + use diesel::associations::HasTable; + diesel_async::RunQueryDsl::get_results( + diesel::dsl::insert_into(#table::table::table()).values(insert), + conn + ) + .await + .map_err(Into::into) } } } diff --git a/crates/diesel_crud_derive/src/delete.rs b/crates/diesel_crud_derive/src/delete.rs index cf553c0..795491c 100644 --- a/crates/diesel_crud_derive/src/delete.rs +++ b/crates/diesel_crud_derive/src/delete.rs @@ -1,4 +1,4 @@ -use crate::{common, Attributes, PrimaryKey}; +use crate::{Attributes, PrimaryKey, common}; use quote::quote; pub(crate) fn derive_diesel_crud_delete_impl( @@ -22,24 +22,18 @@ pub(crate) fn derive_diesel_crud_delete_impl( #[automatically_derived] impl lib::diesel_crud_trait::DieselCrudDelete for #struct_ident { type PK = #pk_type; - fn delete<'a, 'async_trait>(pk: Self::PK, conn: &'a mut diesel_async::AsyncPgConnection) -> #return_type - where - Self: Sized + Sync + 'a, - 'a: 'async_trait, - { - Box::pin(async move { - use diesel::QueryDsl; - use diesel::associations::HasTable; - diesel_async::RunQueryDsl::get_result( - diesel::delete( - #table::table - .filter(diesel::expression_methods::ExpressionMethods::eq(#table::#pk_ident, pk)) - ), - conn, - ) - .await - .map_err(Into::into) - }) + async fn delete(pk: Self::PK, conn: &mut diesel_async::AsyncPgConnection) -> #return_type { + use diesel::QueryDsl; + use diesel::associations::HasTable; + diesel_async::RunQueryDsl::get_result( + diesel::delete( + #table::table + .filter(diesel::expression_methods::ExpressionMethods::eq(#table::#pk_ident, pk)) + ), + conn, + ) + .await + .map_err(Into::into) } } } diff --git a/crates/diesel_crud_derive/src/list.rs b/crates/diesel_crud_derive/src/list.rs index a570172..ec325ee 100644 --- a/crates/diesel_crud_derive/src/list.rs +++ b/crates/diesel_crud_derive/src/list.rs @@ -1,4 +1,4 @@ -use crate::{common, Attributes}; +use crate::{Attributes, common}; use quote::quote; pub(crate) fn derive_diesel_crud_list_impl( @@ -13,15 +13,9 @@ pub(crate) fn derive_diesel_crud_list_impl( quote! { #[automatically_derived] impl lib::diesel_crud_trait::DieselCrudList for #struct_ident { - fn list<'a, 'async_trait>(conn: &'a mut diesel_async::AsyncPgConnection) -> #return_type - where - Self: Sized + Sync + 'a, - 'a: 'async_trait - { - Box::pin(async move { - use diesel::associations::HasTable; - diesel_async::RunQueryDsl::get_results(#table::table::table(), conn).await.map_err(Into::into) - }) + async fn list(conn: &mut diesel_async::AsyncPgConnection) -> #return_type { + use diesel::associations::HasTable; + diesel_async::RunQueryDsl::get_results(#table::table::table(), conn).await.map_err(Into::into) } } } diff --git a/crates/diesel_crud_derive/src/read.rs b/crates/diesel_crud_derive/src/read.rs index 9f7cb5e..edf41ab 100644 --- a/crates/diesel_crud_derive/src/read.rs +++ b/crates/diesel_crud_derive/src/read.rs @@ -1,5 +1,5 @@ use crate::common::PrimaryKey; -use crate::{common, Attributes}; +use crate::{Attributes, common}; use quote::quote; pub(crate) fn derive_diesel_crud_read_impl( @@ -20,20 +20,14 @@ pub(crate) fn derive_diesel_crud_read_impl( #[automatically_derived] impl lib::diesel_crud_trait::DieselCrudRead for #struct_ident { type PK = #pk_type; - fn read<'a, 'async_trait>(pk: Self::PK, conn: &'a mut diesel_async::AsyncPgConnection) -> #return_type - where - Self: Sized + Sync + 'a, - 'a: 'async_trait - { - Box::pin(async move { - use diesel::associations::HasTable; - diesel_async::RunQueryDsl::get_result( - diesel::QueryDsl::find(#table::table::table(), pk), - conn - ) - .await - .map_err(Into::into) - }) + async fn read(pk: Self::PK, conn: &mut diesel_async::AsyncPgConnection) -> #return_type { + use diesel::associations::HasTable; + diesel_async::RunQueryDsl::get_result( + diesel::QueryDsl::find(#table::table::table(), pk), + conn + ) + .await + .map_err(Into::into) } } } diff --git a/crates/diesel_crud_derive/src/update.rs b/crates/diesel_crud_derive/src/update.rs index 8e5d757..0845910 100644 --- a/crates/diesel_crud_derive/src/update.rs +++ b/crates/diesel_crud_derive/src/update.rs @@ -1,4 +1,4 @@ -use crate::{common, Attributes}; +use crate::{Attributes, common}; use quote::quote; pub(crate) fn derive_diesel_crud_update_impl( @@ -15,20 +15,14 @@ pub(crate) fn derive_diesel_crud_update_impl( #[automatically_derived] impl lib::diesel_crud_trait::DieselCrudUpdate for #struct_ident { type Update = #update; - fn update<'a, 'async_trait>(update: Self::Update, conn: &'a mut diesel_async::AsyncPgConnection) -> #return_type - where - Self: Sized + Sync + 'a, - 'a: 'async_trait, - { - Box::pin(async move { - use diesel::associations::HasTable; - diesel_async::RunQueryDsl::get_result( - diesel::dsl::update(#table::table::table()).set(update), - conn, - ) - .await - .map_err(Into::into) - }) + async fn update(update: Self::Update, conn: &mut diesel_async::AsyncPgConnection) -> #return_type { + use diesel::associations::HasTable; + diesel_async::RunQueryDsl::get_result( + diesel::dsl::update(#table::table::table()).set(update), + conn, + ) + .await + .map_err(Into::into) } } } diff --git a/crates/diesel_crud_trait/Cargo.toml b/crates/diesel_crud_trait/Cargo.toml index c6a0521..a64271f 100644 --- a/crates/diesel_crud_trait/Cargo.toml +++ b/crates/diesel_crud_trait/Cargo.toml @@ -7,6 +7,4 @@ rust-version = { workspace = true } [dependencies] diesel = { workspace = true, features = ["postgres"] } diesel-async = { workspace = true, features = ["postgres", "deadpool"] } -async-trait = "0.1" -deadpool-diesel = { version = "0.6", features = ["postgres"] } thiserror = { workspace = true } diff --git a/crates/diesel_crud_trait/src/lib.rs b/crates/diesel_crud_trait/src/lib.rs index 88d04f4..f89ec5d 100644 --- a/crates/diesel_crud_trait/src/lib.rs +++ b/crates/diesel_crud_trait/src/lib.rs @@ -1,6 +1,5 @@ mod error; -use async_trait::async_trait; use diesel::{AsChangeset, Insertable}; use diesel_async::AsyncPgConnection; pub use error::CrudError; @@ -28,17 +27,19 @@ pub trait DieselCrud: /// - `conn` - The database connection /// # Returns /// A result containing the inserted entity or a `CrudError` -#[async_trait] pub trait DieselCrudCreate
where Self: Sized, { type Insert: Insertable
; - async fn insert(insert: Self::Insert, conn: &mut AsyncPgConnection) -> Result; - async fn insert_many( + fn insert( + insert: Self::Insert, + conn: &mut AsyncPgConnection, + ) -> impl Future>; + fn insert_many( insert: &[Self::Insert], conn: &mut AsyncPgConnection, - ) -> Result, CrudError>; + ) -> impl Future, CrudError>>; } /// Gets an entity from the database @@ -52,13 +53,15 @@ where /// # Returns /// A result containing the entity or a `CrudError`. /// If the entity is not found, the error should be `CrudError::NotFound`. -#[async_trait] pub trait DieselCrudRead where Self: Sized, { type PK; - async fn read(pk: Self::PK, conn: &mut AsyncPgConnection) -> Result; + fn read( + pk: Self::PK, + conn: &mut AsyncPgConnection, + ) -> impl Future>; } /// Updates an entity in the database @@ -73,13 +76,15 @@ where /// # Returns /// A result containing the old entry of the entity if successful or a `CrudError`. /// If the entity is not found, the error should be `CrudError::NotFound`. -#[async_trait] pub trait DieselCrudUpdate where Self: Sized, { type Update: AsChangeset; - async fn update(update: Self::Update, conn: &mut AsyncPgConnection) -> Result; + fn update( + update: Self::Update, + conn: &mut AsyncPgConnection, + ) -> impl Future>; } /// Deletes an entity from the database @@ -93,13 +98,15 @@ where /// # Returns /// A result containing the deleted entity or a `CrudError`. /// If the entity is not found, the error should be `CrudError::NotFound`. -#[async_trait] pub trait DieselCrudDelete where Self: Sized, { type PK; - async fn delete(pk: Self::PK, conn: &mut AsyncPgConnection) -> Result; + fn delete( + pk: Self::PK, + conn: &mut AsyncPgConnection, + ) -> impl Future>; } /// Lists all entities in the table @@ -109,10 +116,9 @@ where /// - `conn` - The database connection /// # Returns /// A result containing a Vec of entities or a `CrudError`. -#[async_trait] pub trait DieselCrudList where Self: Sized, { - async fn list(conn: &mut AsyncPgConnection) -> Result, CrudError>; + fn list(conn: &mut AsyncPgConnection) -> impl Future, CrudError>>; } diff --git a/src/axum/response.rs b/src/axum/response.rs index 2e44d5e..39114c6 100644 --- a/src/axum/response.rs +++ b/src/axum/response.rs @@ -1,14 +1,13 @@ use { crate::{serde::response::BaseResponse, serde::traits::DeserializeInto}, - async_trait::async_trait, axum::{ + Json, body::to_bytes, response::{IntoResponse, Response}, - Json, }, serde::{ - de::{DeserializeOwned, Error}, Serialize, + de::{DeserializeOwned, Error}, }, }; @@ -18,7 +17,6 @@ impl IntoResponse for BaseResponse { } } -#[async_trait] impl DeserializeInto for Response { async fn deserialize_into(self) -> Result { let body = to_bytes(self.into_body(), usize::MAX).await.map_err(|e| { diff --git a/src/axum/router.rs b/src/axum/router.rs index a48f284..c40a91b 100644 --- a/src/axum/router.rs +++ b/src/axum/router.rs @@ -13,8 +13,8 @@ /// use lib::router; /// async fn simplify(path: axum::extract::path::Path) {} /// router!("/simplify", lib::routes!( -/// get "/:exp" => simplify, -/// get "/table/:exp" => || async {} +/// get "/{exp}" => simplify, +/// get "/table/{exp}" => async || {} /// )); /// ``` #[macro_export] @@ -92,8 +92,8 @@ macro_rules! join_routes { #[cfg(test)] mod tests { - use axum::extract::State; use axum::Router; + use axum::extract::State; async fn index() {} diff --git a/src/diesel/get_connection.rs b/src/diesel/get_connection.rs index 9e229f1..2bb73fc 100644 --- a/src/diesel/get_connection.rs +++ b/src/diesel/get_connection.rs @@ -1,17 +1,16 @@ -use async_trait::async_trait; use deadpool_diesel::Status; use derive_more::From; use diesel_async::AsyncPgConnection; use diesel_async::pooled_connection::deadpool::{Object, PoolError}; use lib::diesel::pool::PgPool; -#[async_trait] pub trait GetConnection: Clone + Send + Sync { - async fn get(&self) -> Result, GetConnectionError>; + fn get( + &self, + ) -> impl Future, GetConnectionError>> + Send; fn status(&self) -> Status; } -#[async_trait] impl GetConnection for PgPool { async fn get(&self) -> Result, GetConnectionError> { self.get().await.map_err(Into::into) diff --git a/src/serde/traits.rs b/src/serde/traits.rs index 6109e67..1e00f21 100644 --- a/src/serde/traits.rs +++ b/src/serde/traits.rs @@ -1,7 +1,7 @@ -use async_trait::async_trait; use serde::de::DeserializeOwned; -#[async_trait] pub trait DeserializeInto { - async fn deserialize_into(self) -> Result; + fn deserialize_into( + self, + ) -> impl Future>; } diff --git a/src/test/diesel_pool.rs b/src/test/diesel_pool.rs index 07bdc01..eff4aa5 100644 --- a/src/test/diesel_pool.rs +++ b/src/test/diesel_pool.rs @@ -1,7 +1,6 @@ use crate::diesel::DieselError; use crate::diesel::get_connection::{GetConnection, GetConnectionError}; use crate::diesel::pool::PgPool; -use async_trait::async_trait; use deadpool_diesel::Status; use deadpool_diesel::postgres::BuildError; use derive_more::From; @@ -32,7 +31,6 @@ pub async fn create_test_pool_url_with_size( Ok(PoolStub(pool)) } -#[async_trait] impl GetConnection for PoolStub { async fn get(&self) -> Result, GetConnectionError> { let mut conn = self.0.get().await?; diff --git a/src/test/test_containers.rs b/src/test/test_containers.rs index 4e740ad..305630a 100644 --- a/src/test/test_containers.rs +++ b/src/test/test_containers.rs @@ -6,6 +6,7 @@ use lib::diesel::DieselError; use testcontainers_modules::postgres::Postgres; use testcontainers_modules::testcontainers::runners::AsyncRunner; use testcontainers_modules::testcontainers::{ContainerAsync, TestcontainersError}; +use tokio::task::JoinError; /// When the TestContainer is dropped, the container will be removed. /// # Errors @@ -16,11 +17,15 @@ pub struct TestContainer { pub pool: PgPool, } +const TEST_CONTAINERS_INTERNAL_PORT: u16 = 5432; + pub async fn create_test_containers_pool() -> Result { let container = create_postgres_container().await?; let connection_string = format!( "postgres://postgres:postgres@127.0.0.1:{}/postgres", - container.get_host_port_ipv4(5432).await? + container + .get_host_port_ipv4(TEST_CONTAINERS_INTERNAL_PORT) + .await? ); let pool = create_pool_from_url(connection_string)?; Ok(TestContainer::new(container, pool)) @@ -36,4 +41,5 @@ pub enum ContainerError { BuildError(BuildError), PoolError(PoolError), DieselError(DieselError), + JoinError(JoinError), }