From f1bc336d45da7a7051fcc85093569edf63e90f25 Mon Sep 17 00:00:00 2001 From: benluelo Date: Wed, 1 Feb 2023 00:01:39 -0500 Subject: [PATCH] fix: Decode and Encode derives (#1031) --- sqlx-macros/src/derives/decode.rs | 27 ++++++++++++--------------- sqlx-macros/src/derives/encode.rs | 27 ++++++++++++--------------- 2 files changed, 24 insertions(+), 30 deletions(-) diff --git a/sqlx-macros/src/derives/decode.rs b/sqlx-macros/src/derives/decode.rs index f926718f6c..b848c69a19 100644 --- a/sqlx-macros/src/derives/decode.rs +++ b/sqlx-macros/src/derives/decode.rs @@ -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 { @@ -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; diff --git a/sqlx-macros/src/derives/encode.rs b/sqlx-macros/src/derives/encode.rs index 7bb568210f..a70bb079d9 100644 --- a/sqlx-macros/src/derives/encode.rs +++ b/sqlx-macros/src/derives/encode.rs @@ -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 { @@ -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;