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

sqlx::Type macro is expanding code that doesn't compile for Option<T>. #1268

Closed
Wopple opened this issue Jun 2, 2021 · 2 comments
Closed

Comments

@Wopple
Copy link
Contributor

Wopple commented Jun 2, 2021

With this dependency:

sqlx = { version = "0.5.1", features = ["any", "json", "postgres", "runtime-tokio-rustls"] }

This code:

#[derive(sqlx::Type)]
struct AttributeRow {
    kind: String,
    source: Option<String>,
    value: serde_json::Value,
}

Generates this error:

error: implementation of `sqlx::Decode` is not general enough
  --> service/src/recommendations/recommend.rs:20:10
   |
20 | #[derive(sqlx::Type)]
   |          ^^^^^^^^^^ implementation of `sqlx::Decode` is not general enough
   |
   = note: `std::string::String` must implement `sqlx::Decode<'0, sqlx::Postgres>`, for some specific lifetime `'0`...
   = note: ...but it actually implements `sqlx::Decode<'r, sqlx::Postgres>`
   = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

And I worked around it by changing the generated code:

impl<'r> ::sqlx::decode::Decode<'r, ::sqlx::Postgres> for AttributeRow
    where
        String: ::sqlx::decode::Decode<'r, ::sqlx::Postgres>,
        String: ::sqlx::types::Type<::sqlx::Postgres>,
        Option<String>: ::sqlx::decode::Decode<'r, ::sqlx::Postgres>,
        Option<String>: ::sqlx::types::Type<::sqlx::Postgres>,
        serde_json::Value: ::sqlx::decode::Decode<'r, ::sqlx::Postgres>,
        serde_json::Value: ::sqlx::types::Type<::sqlx::Postgres>,
{
    fn decode(
        value: ::sqlx::postgres::PgValueRef<'r>,
    ) -> ::std::result::Result<
        Self,
        ::std::boxed::Box<
            dyn ::std::error::Error + 'static + ::std::marker::Send + ::std::marker::Sync,
        >,
    > {
        let mut decoder = ::sqlx::postgres::types::PgRecordDecoder::new(value)?;
        let kind = decoder.try_decode::<String>()?;
        let source = decoder.try_decode::<Option<String>>()?;
        let value = decoder.try_decode::<serde_json::Value>()?;
        ::std::result::Result::Ok(AttributeRow {
            kind,
            source,
            value,
        })
    }
}

To this:

impl ::sqlx::decode::Decode<'_, ::sqlx::Postgres> for AttributeRow
{
    fn decode(
        value: ::sqlx::postgres::PgValueRef<'_>,
    ) -> ::std::result::Result<
        Self,
        ::std::boxed::Box<
            dyn ::std::error::Error + 'static + ::std::marker::Send + ::std::marker::Sync,
        >,
    > {
        let mut decoder = ::sqlx::postgres::types::PgRecordDecoder::new(value)?;
        let kind = decoder.try_decode::<String>()?;
        let source = decoder.try_decode::<Option<String>>()?;
        let value = decoder.try_decode::<serde_json::Value>()?;
        ::std::result::Result::Ok(AttributeRow {
            kind,
            source,
            value,
        })
    }
}

Note the change in lifetime 'r -> '_ and removal of context bounds.

@jplatte
Copy link
Contributor

jplatte commented Jun 2, 2021

Duplicate of #1031. This is a compiler bug (rust-lang/rust#82219). The bound for Option<String> is fine but in combination with the bound for String, the compiler gets confused.

I wrote about a possible fix in #1031 (comment).

@Wopple
Copy link
Contributor Author

Wopple commented Jun 2, 2021

Thank you.

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

No branches or pull requests

2 participants