AsExpression

Derive Macro AsExpression 

Source
#[derive(AsExpression)]
{
    // Attributes available to this derive:
    #[diesel]
    #[sql_type]
}
Expand description

Implements all required variants of AsExpression

This derive will generate the following impls:

  • impl AsExpression<SqlType> for YourType
  • impl AsExpression<Nullable<SqlType>> for YourType
  • impl AsExpression<SqlType> for &'a YourType
  • impl AsExpression<Nullable<SqlType>> for &'a YourType
  • impl AsExpression<SqlType> for &'a &'b YourType
  • impl AsExpression<Nullable<SqlType>> for &'a &'b YourType

If your type is unsized, you can specify this by adding the annotation #[diesel(not_sized)] as attribute on the type. This will skip the impls for non-reference types.

Using this derive requires implementing the ToSql trait for your type.

§Attributes:

§Required container attributes

  • #[diesel(sql_type = SqlType)], to specify the sql type of the generated implementations. If the attribute exists multiple times impls for each sql type is generated.

§Optional container attributes

  • #[diesel(not_sized)], to skip generating impls that require that the type is Sized

§Expanded Code

Expanded Code
§Input
#[derive(AsExpression)]
#[diesel(sql_type = diesel::sql_type::Integer)]
enum Foo {
    Bar,
    Baz,
}
§Expanded Code
Expanded code might use diesel internal API's and is only shown for educational purpose

The macro expands the input to the following Rust code:

const _: () = {
    use diesel;
    impl<'__expr> diesel::expression::AsExpression<diesel::sql_type::Integer>
    for &'__expr Foo {
        type Expression = diesel::internal::derives::as_expression::Bound<
            diesel::sql_type::Integer,
            Self,
        >;
        fn as_expression(
            self,
        ) -> <Self as diesel::expression::AsExpression<
            diesel::sql_type::Integer,
        >>::Expression {
            diesel::internal::derives::as_expression::Bound::new(self)
        }
    }
    impl<
        '__expr,
    > diesel::expression::AsExpression<
        diesel::sql_types::Nullable<diesel::sql_type::Integer>,
    > for &'__expr Foo {
        type Expression = diesel::internal::derives::as_expression::Bound<
            diesel::sql_types::Nullable<diesel::sql_type::Integer>,
            Self,
        >;
        fn as_expression(
            self,
        ) -> <Self as diesel::expression::AsExpression<
            diesel::sql_types::Nullable<diesel::sql_type::Integer>,
        >>::Expression {
            diesel::internal::derives::as_expression::Bound::new(self)
        }
    }
    impl<'__expr, '__expr2> diesel::expression::AsExpression<diesel::sql_type::Integer>
    for &'__expr2 &'__expr Foo {
        type Expression = diesel::internal::derives::as_expression::Bound<
            diesel::sql_type::Integer,
            Self,
        >;
        fn as_expression(
            self,
        ) -> <Self as diesel::expression::AsExpression<
            diesel::sql_type::Integer,
        >>::Expression {
            diesel::internal::derives::as_expression::Bound::new(self)
        }
    }
    impl<
        '__expr,
        '__expr2,
    > diesel::expression::AsExpression<
        diesel::sql_types::Nullable<diesel::sql_type::Integer>,
    > for &'__expr2 &'__expr Foo {
        type Expression = diesel::internal::derives::as_expression::Bound<
            diesel::sql_types::Nullable<diesel::sql_type::Integer>,
            Self,
        >;
        fn as_expression(
            self,
        ) -> <Self as diesel::expression::AsExpression<
            diesel::sql_types::Nullable<diesel::sql_type::Integer>,
        >>::Expression {
            diesel::internal::derives::as_expression::Bound::new(self)
        }
    }
    impl<
        __DB,
    > diesel::serialize::ToSql<
        diesel::sql_types::Nullable<diesel::sql_type::Integer>,
        __DB,
    > for Foo
    where
        __DB: diesel::backend::Backend,
        Self: diesel::serialize::ToSql<diesel::sql_type::Integer, __DB>,
    {
        fn to_sql<'__b>(
            &'__b self,
            out: &mut diesel::serialize::Output<'__b, '_, __DB>,
        ) -> diesel::serialize::Result {
            diesel::serialize::ToSql::<
                diesel::sql_type::Integer,
                __DB,
            >::to_sql(self, out)
        }
    }
    impl diesel::expression::AsExpression<diesel::sql_type::Integer> for Foo {
        type Expression = diesel::internal::derives::as_expression::Bound<
            diesel::sql_type::Integer,
            Self,
        >;
        fn as_expression(
            self,
        ) -> <Self as diesel::expression::AsExpression<
            diesel::sql_type::Integer,
        >>::Expression {
            diesel::internal::derives::as_expression::Bound::new(self)
        }
    }
    impl diesel::expression::AsExpression<
        diesel::sql_types::Nullable<diesel::sql_type::Integer>,
    > for Foo {
        type Expression = diesel::internal::derives::as_expression::Bound<
            diesel::sql_types::Nullable<diesel::sql_type::Integer>,
            Self,
        >;
        fn as_expression(
            self,
        ) -> <Self as diesel::expression::AsExpression<
            diesel::sql_types::Nullable<diesel::sql_type::Integer>,
        >>::Expression {
            diesel::internal::derives::as_expression::Bound::new(self)
        }
    }
};