Skip to content

Commit 96f14c0

Browse files
committed
Merge remote-tracking branch 'origin/ab/proc-macro-unhack-sort-of' into rl-transaction-1
2 parents 6ddf318 + 0fdb875 commit 96f14c0

File tree

17 files changed

+251
-226
lines changed

17 files changed

+251
-226
lines changed

Cargo.lock

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ all-features = true
2929

3030
[features]
3131
default = [ "macros" ]
32-
macros = [ "sqlx-macros", "proc-macro-hack" ]
32+
macros = [ "sqlx-macros" ]
3333
tls = ["sqlx-core/tls"]
3434

3535
# database
@@ -43,7 +43,6 @@ uuid = [ "sqlx-core/uuid", "sqlx-macros/uuid" ]
4343
[dependencies]
4444
sqlx-core = { version = "=0.1.4", path = "sqlx-core" }
4545
sqlx-macros = { version = "0.1.1", path = "sqlx-macros", optional = true }
46-
proc-macro-hack = { version = "0.5.11", optional = true }
4746

4847
[dev-dependencies]
4948
anyhow = "1.0.26"

sqlx-core/src/arguments.rs

Lines changed: 9 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -38,123 +38,22 @@ where
3838
fn into_arguments(self) -> DB::Arguments;
3939
}
4040

41-
impl<DB> IntoArguments<DB> for DB::Arguments
41+
impl<A> IntoArguments<A::Database> for A
4242
where
43-
DB: Database,
43+
A: Arguments,
44+
A::Database: Database<Arguments = Self> + Sized,
4445
{
4546
#[inline]
46-
fn into_arguments(self) -> DB::Arguments {
47+
fn into_arguments(self) -> Self {
4748
self
4849
}
4950
}
5051

51-
#[allow(unused)]
52-
macro_rules! impl_into_arguments {
53-
($B:ident: $( ($idx:tt) -> $T:ident );+;) => {
54-
impl<$($T,)+> crate::arguments::IntoArguments<$B> for ($($T,)+)
55-
where
56-
$($B: crate::types::HasSqlType<$T>,)+
57-
$($T: crate::encode::Encode<$B>,)+
58-
{
59-
fn into_arguments(self) -> <$B as crate::database::Database>::Arguments {
60-
use crate::arguments::Arguments;
61-
62-
let mut arguments = <$B as crate::database::Database>::Arguments::default();
63-
64-
let binds = 0 $(+ { $idx; 1 } )+;
65-
let bytes = 0 $(+ crate::encode::Encode::size_hint(&self.$idx))+;
66-
67-
arguments.reserve(binds, bytes);
68-
69-
$(crate::arguments::Arguments::add(&mut arguments, self.$idx);)+
70-
71-
arguments
72-
}
73-
}
74-
};
75-
}
76-
77-
#[allow(unused)]
78-
macro_rules! impl_into_arguments_for_database {
79-
($B:ident) => {
80-
impl crate::arguments::IntoArguments<$B> for ()
81-
{
82-
#[inline]
83-
fn into_arguments(self) -> <$B as crate::database::Database>::Arguments {
84-
Default::default()
85-
}
86-
}
87-
88-
impl_into_arguments!($B:
89-
(0) -> T1;
90-
);
91-
92-
impl_into_arguments!($B:
93-
(0) -> T1;
94-
(1) -> T2;
95-
);
96-
97-
impl_into_arguments!($B:
98-
(0) -> T1;
99-
(1) -> T2;
100-
(2) -> T3;
101-
);
102-
103-
impl_into_arguments!($B:
104-
(0) -> T1;
105-
(1) -> T2;
106-
(2) -> T3;
107-
(3) -> T4;
108-
);
109-
110-
impl_into_arguments!($B:
111-
(0) -> T1;
112-
(1) -> T2;
113-
(2) -> T3;
114-
(3) -> T4;
115-
(4) -> T5;
116-
);
117-
118-
impl_into_arguments!($B:
119-
(0) -> T1;
120-
(1) -> T2;
121-
(2) -> T3;
122-
(3) -> T4;
123-
(4) -> T5;
124-
(5) -> T6;
125-
);
126-
127-
impl_into_arguments!($B:
128-
(0) -> T1;
129-
(1) -> T2;
130-
(2) -> T3;
131-
(3) -> T4;
132-
(4) -> T5;
133-
(5) -> T6;
134-
(6) -> T7;
135-
);
136-
137-
impl_into_arguments!($B:
138-
(0) -> T1;
139-
(1) -> T2;
140-
(2) -> T3;
141-
(3) -> T4;
142-
(4) -> T5;
143-
(5) -> T6;
144-
(6) -> T7;
145-
(7) -> T8;
146-
);
52+
#[doc(hidden)]
53+
pub struct ImmutableArguments<DB: Database>(pub DB::Arguments);
14754

148-
impl_into_arguments!($B:
149-
(0) -> T1;
150-
(1) -> T2;
151-
(2) -> T3;
152-
(3) -> T4;
153-
(4) -> T5;
154-
(5) -> T6;
155-
(6) -> T7;
156-
(7) -> T8;
157-
(8) -> T9;
158-
);
55+
impl<DB: Database> IntoArguments<DB> for ImmutableArguments<DB> {
56+
fn into_arguments(self) -> <DB as Database>::Arguments {
57+
self.0
15958
}
16059
}

sqlx-core/src/mysql/database.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,3 @@ impl Database for MySql {
1414

1515
type TableId = Box<str>;
1616
}
17-
18-
impl_into_arguments_for_database!(MySql);

sqlx-core/src/postgres/arguments.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use byteorder::{ByteOrder, NetworkEndian};
22

3-
use crate::arguments::Arguments;
3+
use crate::arguments::{Arguments, IntoArguments};
44
use crate::encode::{Encode, IsNull};
55
use crate::io::BufMut;
66
use crate::types::HasSqlType;

sqlx-core/src/postgres/database.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,3 @@ impl Database for Postgres {
1414

1515
type TableId = u32;
1616
}
17-
18-
impl_into_arguments_for_database!(Postgres);

sqlx-core/src/query_as.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use futures_core::Stream;
22
use futures_util::{future, TryStreamExt};
33

4-
use crate::arguments::Arguments;
4+
use crate::arguments::{Arguments, ImmutableArguments};
55
use crate::{
66
arguments::IntoArguments, database::Database, encode::Encode, executor::Executor, row::FromRow,
77
types::HasSqlType,
@@ -128,13 +128,10 @@ where
128128

129129
// used by query!() and friends
130130
#[doc(hidden)]
131-
pub fn bind_all<I>(self, values: I) -> QueryAs<'q, DB, R, I>
132-
where
133-
I: IntoArguments<DB>,
134-
{
131+
pub fn bind_all(self, values: DB::Arguments) -> QueryAs<'q, DB, R, ImmutableArguments<DB>> {
135132
QueryAs {
136133
query: self.query,
137-
args: values,
134+
args: ImmutableArguments(values),
138135
map_row: self.map_row,
139136
}
140137
}

sqlx-macros/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ uuid = [ "sqlx/uuid" ]
3333
async-std = { version = "1.4.0", default-features = false }
3434
dotenv = { version = "0.15.0", default-features = false }
3535
futures = { version = "0.3.1", default-features = false }
36-
proc-macro-hack = { version = "0.5.11", default-features = false }
3736
proc-macro2 = { version = "1.0.6", default-features = false }
3837
sqlx = { version = "0.1.1", default-features = false, path = "../sqlx-core", package = "sqlx-core" }
3938
syn = { version = "1.0.11", default-features = false, features = [ "full" ] }

sqlx-macros/src/lib.rs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ extern crate proc_macro;
66

77
use proc_macro::TokenStream;
88

9-
use proc_macro_hack::proc_macro_hack;
10-
119
use quote::quote;
1210

1311
use syn::parse_macro_input;
@@ -26,8 +24,22 @@ mod query_macros;
2624

2725
use query_macros::*;
2826

27+
fn macro_result(tokens: proc_macro2::TokenStream) -> TokenStream {
28+
quote!(
29+
macro_rules! macro_result {
30+
($($args:tt)*) => (#tokens)
31+
}
32+
)
33+
.into()
34+
}
35+
2936
macro_rules! async_macro (
30-
($db:ident => $expr:expr) => {{
37+
($db:ident, $input:ident: $ty:ty => $expr:expr) => {{
38+
let $input = match syn::parse::<$ty>($input) {
39+
Ok(input) => input,
40+
Err(e) => return macro_result(e.to_compile_error()),
41+
};
42+
3143
let res: Result<proc_macro2::TokenStream> = task::block_on(async {
3244
use sqlx::Connect;
3345

@@ -70,40 +82,36 @@ macro_rules! async_macro (
7082
Ok(ts) => ts.into(),
7183
Err(e) => {
7284
if let Some(parse_err) = e.downcast_ref::<syn::Error>() {
73-
return parse_err.to_compile_error().into();
85+
macro_result(parse_err.to_compile_error())
86+
} else {
87+
let msg = format!("{:?}", e);
88+
macro_result(quote!(compile_error!(#msg)))
7489
}
75-
76-
let msg = format!("{:?}", e);
77-
quote!(compile_error!(#msg);).into()
7890
}
7991
}
8092
}}
8193
);
8294

83-
#[proc_macro_hack]
95+
#[proc_macro]
8496
pub fn query(input: TokenStream) -> TokenStream {
8597
#[allow(unused_variables)]
86-
let input = parse_macro_input!(input as QueryMacroInput);
87-
async_macro!(db => expand_query(input, db))
98+
async_macro!(db, input: QueryMacroInput => expand_query(input, db))
8899
}
89100

90-
#[proc_macro_hack]
101+
#[proc_macro]
91102
pub fn query_file(input: TokenStream) -> TokenStream {
92103
#[allow(unused_variables)]
93-
let input = parse_macro_input!(input as QueryMacroInput);
94-
async_macro!(db => expand_query_file(input, db))
104+
async_macro!(db, input: QueryMacroInput => expand_query_file(input, db))
95105
}
96106

97-
#[proc_macro_hack]
107+
#[proc_macro]
98108
pub fn query_as(input: TokenStream) -> TokenStream {
99109
#[allow(unused_variables)]
100-
let input = parse_macro_input!(input as QueryAsMacroInput);
101-
async_macro!(db => expand_query_as(input, db))
110+
async_macro!(db, input: QueryAsMacroInput => expand_query_as(input, db))
102111
}
103112

104-
#[proc_macro_hack]
113+
#[proc_macro]
105114
pub fn query_file_as(input: TokenStream) -> TokenStream {
106115
#[allow(unused_variables)]
107-
let input = parse_macro_input!(input as QueryAsMacroInput);
108-
async_macro!(db => expand_query_file_as(input, db))
116+
async_macro!(db, input: QueryAsMacroInput => expand_query_file_as(input, db))
109117
}

sqlx-macros/src/query_macros/args.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub fn quote_args<DB: DatabaseExt>(
1212
input: &QueryMacroInput,
1313
describe: &Describe<DB>,
1414
) -> crate::Result<TokenStream> {
15-
if input.args.is_empty() {
15+
if input.arg_names.is_empty() {
1616
return Ok(quote! {
1717
let args = ();
1818
});
@@ -22,7 +22,7 @@ pub fn quote_args<DB: DatabaseExt>(
2222
let param_types = describe
2323
.param_types
2424
.iter()
25-
.zip(&*input.args)
25+
.zip(&*input.arg_exprs)
2626
.map(|(type_, expr)| {
2727
get_type_override(expr)
2828
.or_else(|| {
@@ -36,7 +36,7 @@ pub fn quote_args<DB: DatabaseExt>(
3636
})
3737
.collect::<crate::Result<Vec<_>>>()?;
3838

39-
let args_ty_cons = input.args.iter().enumerate().map(|(i, expr)| {
39+
let args_ty_cons = input.arg_names.iter().enumerate().map(|(i, expr)| {
4040
// required or `quote!()` emits it as `Nusize`
4141
let i = syn::Index::from(i);
4242
quote_spanned!( expr.span() => {
@@ -56,10 +56,11 @@ pub fn quote_args<DB: DatabaseExt>(
5656
TokenStream::new()
5757
};
5858

59-
let args = input.args.iter();
59+
let args = input.arg_names.iter();
6060

6161
Ok(quote! {
62-
let args = (#(&#args),*,);
62+
// emit as a tuple first so each expression is only evaluated once
63+
let args = (#(&$#args),*,);
6364
#args_check
6465
})
6566
}

0 commit comments

Comments
 (0)