Skip to content

Commit

Permalink
fix: Decode and Encode derives (#1031)
Browse files Browse the repository at this point in the history
  • Loading branch information
benluelo committed Dec 18, 2023
1 parent 929af41 commit 3beedf3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 30 deletions.
27 changes: 12 additions & 15 deletions sqlx-macros-core/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-core/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

0 comments on commit 3beedf3

Please sign in to comment.