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

fix: Decode and Encode derives (#1031) #2329

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions sqlx-macros/src/derives/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use syn::punctuated::Punctuated;
use syn::token::Comma;
use syn::{
parse_quote, Arm, Data, DataEnum, DataStruct, DeriveInput, Field, Fields, FieldsNamed,
FieldsUnnamed, Stmt, Variant,
FieldsUnnamed, Stmt, TypeParamBound, Variant,
};

pub fn expand_derive_decode(input: &DeriveInput) -> syn::Result<TokenStream> {
Expand Down Expand Up @@ -265,24 +265,21 @@ fn expand_derive_decode_struct(
if cfg!(feature = "postgres") {
let ident = &input.ident;

// extract type generics
let generics = &input.generics;
let (_, ty_generics, _) = generics.split_for_impl();
let (_, ty_generics, where_clause) = input.generics.split_for_impl();

// add db type for impl generics & where clause
let mut generics = generics.clone();
generics.params.insert(0, parse_quote!('r));

let predicates = &mut generics.make_where_clause().predicates;

for field in fields {
let ty = &field.ty;
let mut generics = input.generics.clone();

predicates.push(parse_quote!(#ty: ::sqlx::decode::Decode<'r, ::sqlx::Postgres>));
predicates.push(parse_quote!(#ty: ::sqlx::types::Type<::sqlx::Postgres>));
// add db type for impl generics & where clause
for type_param in &mut generics.type_params_mut() {
type_param.bounds.extend::<[TypeParamBound; 2]>([
parse_quote!(for<'decode> ::sqlx::decode::Decode<'decode, ::sqlx::Postgres>),
parse_quote!(::sqlx::types::Type<::sqlx::Postgres>),
]);
}

let (impl_generics, _, where_clause) = generics.split_for_impl();
generics.params.push(parse_quote!('r));

let (impl_generics, _, _) = generics.split_for_impl();

let reads = fields.iter().map(|field| -> Stmt {
let id = &field.ident;
Expand Down
27 changes: 12 additions & 15 deletions sqlx-macros/src/derives/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use syn::punctuated::Punctuated;
use syn::token::Comma;
use syn::{
parse_quote, Data, DataEnum, DataStruct, DeriveInput, Expr, Field, Fields, FieldsNamed,
FieldsUnnamed, Lifetime, LifetimeDef, Stmt, Variant,
FieldsUnnamed, Lifetime, LifetimeDef, Stmt, TypeParamBound, Variant,
};

pub fn expand_derive_encode(input: &DeriveInput) -> syn::Result<TokenStream> {
Expand Down Expand Up @@ -205,24 +205,21 @@ fn expand_derive_encode_struct(
let ident = &input.ident;
let column_count = fields.len();

// extract type generics
let generics = &input.generics;
let (_, ty_generics, _) = generics.split_for_impl();
let (_, ty_generics, where_clause) = input.generics.split_for_impl();

// add db type for impl generics & where clause
let mut generics = generics.clone();

let predicates = &mut generics.make_where_clause().predicates;

for field in fields {
let ty = &field.ty;
let mut generics = input.generics.clone();

predicates
.push(parse_quote!(#ty: for<'q> ::sqlx::encode::Encode<'q, ::sqlx::Postgres>));
predicates.push(parse_quote!(#ty: ::sqlx::types::Type<::sqlx::Postgres>));
// add db type for impl generics & where clause
for type_param in &mut generics.type_params_mut() {
type_param.bounds.extend::<[TypeParamBound; 2]>([
parse_quote!(for<'encode> ::sqlx::encode::Encode<'encode, ::sqlx::Postgres>),
parse_quote!(::sqlx::types::Type<::sqlx::Postgres>),
]);
}

let (impl_generics, _, where_clause) = generics.split_for_impl();
generics.params.push(parse_quote!('q));

let (impl_generics, _, _) = generics.split_for_impl();

let writes = fields.iter().map(|field| -> Stmt {
let id = &field.ident;
Expand Down