You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to implement a generic trait for diesel. This trait would contains some function definition that needs to be implemented for struct implementing the trait and some function ready-to-use as they would be generic.
I'm having an issue with the exists generic function implementation. More specifically, the filter part is what's not working.
For context, here's the current state of that trait:
use diesel::{
associations::HasTable, dsl, query_builder::IntoUpdateTarget, query_dsl::methods::{FindDsl,LimitDsl}, sql_types::Bool,Expression};use diesel_async::{methods::LoadQuery,AsyncPgConnection,RunQueryDsl};usecrate::{db::utils::PgConn, utils::errors};typeFindId<T> = dsl::Find<<TasHasTable>::Table, <TasCrudId>::IdType>;pubtraitCrudId:HasTable + Sized + SendwhereSelf::Table:FindDsl<Self::IdType>,FindId<Self>:LimitDsl + IntoUpdateTarget + Send,
dsl::Limit<FindId<Self>>:LoadQuery<'static,AsyncPgConnection,Self> + Send + 'static,{typeDTOForm;typeInsertForm<'a>;typeUpdateForm<'a>;typeIdType:Send;// Unable to impl generic function as it create a real lifetime mess...asyncfncreate(new:&Self::InsertForm<'_>,conn:&mutPgConn) -> Result<Self, errors::PError>;asyncfnget_raw_by_id(id:Self::IdType,conn:&mutPgConn) -> Result<Self, errors::PError>{Self::table().find(id).first(conn).await.map_err(errors::PError::from)}// Never able to make it works, don't know why but never figured it outasyncfnget_by_id(id:Self::IdType,conn:&mutPgConn,) -> Result<Self::DTOForm, errors::PError>;// Unable to impl generic function as it create a real lifetime mess...asyncfnupdate(id:Self::IdType,form:&Self::UpdateForm<'_>,conn:&mutPgConn,) -> Result<Self::DTOForm, errors::PError>;asyncfnexists<F>(filter:F,conn:&mutPgConn,) -> Result<bool, errors::PError>whereF:FnOnce(&Self::Table) -> Box<dynExpression<SqlType = Bool> + Send>,{use diesel::QueryDsl;let table = Self::table();let condition = filter(&table);
diesel::select(diesel::dsl::exists(table.filter(condition))).get_result::<bool>(conn).await.map_err(errors::PError::from)}}
And the error I'm getting doesn't really help.. see:
error[E0275]: overflow evaluating the requirement `_: std::marker::Sized`
--> src/traits/crud_id.rs:53:50
|
53 | diesel::select(diesel::dsl::exists(table.filter(condition)))
| ^^^^^^
|
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`my_diesel_generic_trait`)
= note: required for `<<Self as HasTable>::Table as AsQuery>::Query` to implement `FilterDsl<_>`
For more information about this error, try `rustc --explain E0275`.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hey everyone,
I'm trying to implement a generic trait for diesel. This trait would contains some function definition that needs to be implemented for struct implementing the trait and some function ready-to-use as they would be generic.
I'm having an issue with the exists generic function implementation. More specifically, the filter part is what's not working.
For context, here's the current state of that trait:
And the error I'm getting doesn't really help.. see:
Any help would be appreciated :)
Beta Was this translation helpful? Give feedback.
All reactions