From 1b4ca321c3d2a1d045e2f4c542c593582e9c6d80 Mon Sep 17 00:00:00 2001 From: dull b Date: Wed, 26 Jul 2023 03:18:55 +0000 Subject: [PATCH] Attempt separate CrudBounds --- crates/db_schema/src/traits.rs | 73 ++++++++++++++++++++++++++-------- 1 file changed, 57 insertions(+), 16 deletions(-) diff --git a/crates/db_schema/src/traits.rs b/crates/db_schema/src/traits.rs index 3f62f81ae..12175477b 100644 --- a/crates/db_schema/src/traits.rs +++ b/crates/db_schema/src/traits.rs @@ -28,10 +28,62 @@ use std::{hash::Hash, pin::Pin}; /// Returned by `diesel::delete` pub type Delete = DeleteStatement<::Table, ::WhereClause>; -pub type Find<'a, T> = dsl::Find<::Table, >::IdType>; +pub type Find = dsl::Find<::Table, IdType>; -pub type InsertValues<'a, 'b, T> = - <&'a >::InsertForm as Insertable<::Table>>::Values; +pub type InsertValues<'a, T, InsertForm> = + <&'a InsertForm as Insertable<::Table>>::Values; + +pub trait CrudBounds<'a, InsertForm, UpdateForm, IdType> +where + Self: HasTable + Sized, + Self::Table: FindDsl + 'static, + Find: LimitDsl + Send + IntoUpdateTarget + 'static, + dsl::Limit>: Send + LoadQuery<'static, AsyncPgConnection, Self> + 'static, + ::PrimaryKey: ExpressionMethods + Send, + <::PrimaryKey as Expression>::SqlType: SqlType + TypedExpressionType, + Delete>: ExecuteDsl + Send + 'static, + as IntoUpdateTarget>::WhereClause: 'static + Send, + as HasTable>::Table: 'static + Send, + &'a InsertForm: Insertable, + InsertValues<'a, Self, InsertForm>: 'a, + InsertStatement>: + LoadQuery<'a, AsyncPgConnection, Self> + 'a + Send, + InsertForm: 'static + Send + Sync, + UpdateForm: 'static + Send + Sync, + IdType: 'static + + Hash + + Eq + + Sized + + Send + + AsExpression<<::PrimaryKey as Expression>::SqlType>, +{ +} + +impl<'a, InsertForm, UpdateForm, IdType, T> CrudBounds<'a, InsertForm, UpdateForm, IdType> for T +where + Self: HasTable + Sized, + Self::Table: FindDsl + 'static, + Find: LimitDsl + Send + IntoUpdateTarget + 'static, + dsl::Limit>: Send + LoadQuery<'static, AsyncPgConnection, Self> + 'static, + ::PrimaryKey: ExpressionMethods + Send, + <::PrimaryKey as Expression>::SqlType: SqlType + TypedExpressionType, + Delete>: ExecuteDsl + Send + 'static, + as IntoUpdateTarget>::WhereClause: 'static + Send, + as HasTable>::Table: 'static + Send, + &'a InsertForm: Insertable, + InsertValues<'a, Self, InsertForm>: 'a, + InsertStatement>: + LoadQuery<'a, AsyncPgConnection, Self> + 'a + Send, + InsertForm: 'static + Send + Sync, + UpdateForm: 'static + Send + Sync, + IdType: 'static + + Hash + + Eq + + Sized + + Send + + AsExpression<<::PrimaryKey as Expression>::SqlType>, +{ +} // When using `RunQueryDsl::execute`, directly building futures with `Box::pin` and `TryFutureExt::and_then` // instead of `async` + `await` fixes weird compile errors. @@ -41,19 +93,8 @@ pub type InsertValues<'a, 'b, T> = #[async_trait] pub trait Crud<'a> where - Self: HasTable + Sized, - for<'b> Self::Table: FindDsl<>::IdType> + 'static, - for<'b> Find<'b, Self>: LimitDsl + Send + IntoUpdateTarget + 'static, - for<'b> dsl::Limit>: Send + LoadQuery<'static, AsyncPgConnection, Self> + 'static, - ::PrimaryKey: ExpressionMethods + Send, - <::PrimaryKey as Expression>::SqlType: SqlType + TypedExpressionType, - for<'b> Delete>: ExecuteDsl + Send + 'static, - for<'b> as IntoUpdateTarget>::WhereClause: 'static + Send, - for<'b> as HasTable>::Table: 'static + Send, - for<'b> &'a >::InsertForm: Insertable, - for<'b> InsertValues<'a, 'b, Self>: 'a, - for<'b, 'query> InsertStatement>: - LoadQuery<'query, AsyncPgConnection, Self> + 'query + Send, + for<'b> Self: CrudBounds<'b, Self::InsertForm, Self::UpdateForm, Self::IdType>, + Self: Sized, { type InsertForm: 'static + Send + Sync; type UpdateForm: 'static + Send + Sync;