-
I have a trait which generates A reduced variant #[macro_use] extern crate diesel as _;
use diesel::dsl::{And, Eq, AsExprOf};
use diesel::sql_types::{ Bool };
use diesel::*;
diesel::table! {
foo (id) {
id -> Int4,
data -> Nullable<Int4>,
}
}
pub trait EqNull
where
Self: diesel::expression_methods::ExpressionMethods + Clone,
// Self::SqlType: SqlType, // trivial 2.0.0 change
{
fn eq_null<T>(self, v: Option<T>) ->
And<AsExprOf<bool, Bool>, Eq<Self, AsExprOf<Option<T>, Self::SqlType>>>
where
Option<T>: diesel::expression::AsExpression<Self::SqlType>,
{
// reduced example; full implementation contains another 'v.is_none().and(self.is_null())' OR branch
v.is_some().into_sql::<Bool>().and(self.eq(v))
}
}
impl EqNull for foo::columns::data {}
fn _foo()
{
foo::data.eq_null(Some(23));
}
fn main()
{
} compiles with 1.4.6 but with 2.0.0 I am lost in a jungle of How can I express this with diesel 2.0.0? Does diesel 2 perhaps already contain another solution for the underlying problem (comparing with |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
This is now your forth question about how to port an application specific pattern to a unreleased diesel version. Please keep in mind that we generally do not provide any support for unreleased versions. We are happy to receive feedback about unreleased features, but it is generally expected that the documentation and related stuff is not comparable with a finished release yet. |
Beta Was this translation helpful? Give feedback.
-
fwiw, trick was to constrain the associated pub trait EqNull
where
Self: diesel::expression_methods::ExpressionMethods + Clone,
Self::SqlType: diesel::sql_types::SqlType<IsNull = is_nullable::IsNullable>,
{
#[allow(clippy::type_complexity)]
fn eq_null<U>(self, v: Option<U>) ->
Or<And<BoolBound, IsNull<Self>>,
And<BoolBound, Eq<Self, AsExprOf<Option<U>, Self::SqlType>>, Nullable<Bool>>,
Nullable<Bool>>
where
Option<U>: diesel::expression::AsExpression<Self::SqlType>,
{
use diesel::{ IntoSql, BoolExpressionMethods };
let fn_bool = |v: bool| v.into_sql::<Bool>();
fn_bool(v.is_none()).and(self.clone().is_null())
.or(fn_bool(v.is_some()).and(self.eq(v)))
}
} |
Beta Was this translation helpful? Give feedback.
fwiw, trick was to constrain the associated
IsNull
type and the final solution is