Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support newtype refs by mirroring diesel's AsExpression derive #30

Merged
merged 2 commits into from
Mar 17, 2024

Conversation

ethan-lowman-fp
Copy link
Contributor

I noticed that the following type of Insertable struct was not supported:

#[derive(Debug, Clone, PartialEq, Insertable)]
#[diesel(table_name = my_entities)]
pub struct NewMyEntity<'a> {
    id: &'a MyIdString,
    val: i32,
}

This yields the following compiler error when I run tests:

error[E0277]: the trait bound `MyIdString: diesel::Expression` is not satisfied
  --> tests/db-roundtrips.rs:31:5
   |
31 |     id: &'a MyIdString,
   |     ^^ the trait `diesel::Expression` is not implemented for `MyIdString`
   |
   = help: the following other types implement trait `diesel::Expression`:
             Box<T>
             columns::val
             columns::my_nullable_i32
             columns::my_nullable_string
             columns::my_i32
             columns::id
             columns::star
             expression::ops::numeric::Add<Lhs, Rhs>
           and 67 others
   = note: required for `&'a MyIdString` to implement `diesel::Expression`
   = note: required for `&'insert &'a MyIdString` to implement `AsExpression<diesel::sql_types::Text>`

The interesting part here is the last line, where &'insert &'a MyIdString must be AsExpression<diesel::sql_types::Text>. If you inspect Diesel's AsExpression macro [here](https://github.com/diesel-rs/diesel/blob/7bd8b8f208046188e24d5dab2cd12aebba10846d/diesel_derives/src/as_expression.rs#L63-L71 , we see that this is implemented when you #[derive(AsExpression)].

impl #impl_generics2 AsExpression<#sql_type>
    for &'__expr2 &'__expr #struct_ty #where_clause2
{
    type Expression = Bound<#sql_type, Self>;

    fn as_expression(self) -> Self::Expression {
        Bound::new(self)
    }
}

This PR adds in this double-reference implementation. Additionally, to make this work I mirrored another part of Diesel's derive, using Self/self in place of explicit types. I'm not familiar enough with Diesels' internals to know what Bound does exactly, but matching Diesel's macro seems to maintain functionality.

I also noticed that Diesel's AsExpression macro had some special implementations for Nullable that this crate doesn't replicate. To double check that this worked properly, I added some tests that used Nullable types. We seem to get those implicitly here without any extra implementation, I think from this built-in impl paired with this crate's generic approach.

src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Show resolved Hide resolved
@quodlibetor quodlibetor merged commit 23001b5 into quodlibetor:main Mar 17, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants