Skip to main content

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.

Rc<T>, Arc<T>, and Box<T> wrappers around Diesel’s built-in primitive types (String, i32, bool, Vec<u8>, etc.) are supported as AsExpression values through Diesel’s internal foreign_derive implementations, so fields like Rc<String> work transparently with #[derive(Insertable)] and #[derive(Queryable)]. The unsized variants Rc<str> / Arc<str> / Box<str> and the [u8] equivalents are supported as well (one heap allocation instead of two compared to Rc<String>). Wrapping a user-defined AsExpression type in Rc/Arc/Box is not currently supported because of coherence and orphan-rule conflicts between the wildcard impl<T, ST> AsExpression<ST> for T where T: Expression<SqlType = ST> and the smart-pointer Expression impls.

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
  • #[diesel(enum_type)], to indicate that the type represents a SQL side enum