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

WIP: strip out proc-macro-hack #51

Merged
merged 2 commits into from
Jan 15, 2020
Merged
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
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ all-features = true

[features]
default = [ "macros" ]
macros = [ "sqlx-macros", "proc-macro-hack" ]
macros = [ "sqlx-macros" ]
tls = ["sqlx-core/tls"]

# database
Expand All @@ -43,7 +43,6 @@ uuid = [ "sqlx-core/uuid", "sqlx-macros/uuid" ]
[dependencies]
sqlx-core = { version = "=0.1.4", path = "sqlx-core" }
sqlx-macros = { version = "0.1.1", path = "sqlx-macros", optional = true }
proc-macro-hack = { version = "0.5.11", optional = true }

[dev-dependencies]
anyhow = "1.0.26"
Expand Down
119 changes: 9 additions & 110 deletions sqlx-core/src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,123 +38,22 @@ where
fn into_arguments(self) -> DB::Arguments;
}

impl<DB> IntoArguments<DB> for DB::Arguments
impl<A> IntoArguments<A::Database> for A
where
DB: Database,
A: Arguments,
A::Database: Database<Arguments = Self> + Sized,
{
#[inline]
fn into_arguments(self) -> DB::Arguments {
fn into_arguments(self) -> Self {
self
}
}

#[allow(unused)]
macro_rules! impl_into_arguments {
($B:ident: $( ($idx:tt) -> $T:ident );+;) => {
impl<$($T,)+> crate::arguments::IntoArguments<$B> for ($($T,)+)
where
$($B: crate::types::HasSqlType<$T>,)+
$($T: crate::encode::Encode<$B>,)+
{
fn into_arguments(self) -> <$B as crate::database::Database>::Arguments {
use crate::arguments::Arguments;

let mut arguments = <$B as crate::database::Database>::Arguments::default();

let binds = 0 $(+ { $idx; 1 } )+;
let bytes = 0 $(+ crate::encode::Encode::size_hint(&self.$idx))+;

arguments.reserve(binds, bytes);

$(crate::arguments::Arguments::add(&mut arguments, self.$idx);)+

arguments
}
}
};
}

#[allow(unused)]
macro_rules! impl_into_arguments_for_database {
($B:ident) => {
impl crate::arguments::IntoArguments<$B> for ()
{
#[inline]
fn into_arguments(self) -> <$B as crate::database::Database>::Arguments {
Default::default()
}
}

impl_into_arguments!($B:
(0) -> T1;
);

impl_into_arguments!($B:
(0) -> T1;
(1) -> T2;
);

impl_into_arguments!($B:
(0) -> T1;
(1) -> T2;
(2) -> T3;
);

impl_into_arguments!($B:
(0) -> T1;
(1) -> T2;
(2) -> T3;
(3) -> T4;
);

impl_into_arguments!($B:
(0) -> T1;
(1) -> T2;
(2) -> T3;
(3) -> T4;
(4) -> T5;
);

impl_into_arguments!($B:
(0) -> T1;
(1) -> T2;
(2) -> T3;
(3) -> T4;
(4) -> T5;
(5) -> T6;
);

impl_into_arguments!($B:
(0) -> T1;
(1) -> T2;
(2) -> T3;
(3) -> T4;
(4) -> T5;
(5) -> T6;
(6) -> T7;
);

impl_into_arguments!($B:
(0) -> T1;
(1) -> T2;
(2) -> T3;
(3) -> T4;
(4) -> T5;
(5) -> T6;
(6) -> T7;
(7) -> T8;
);
#[doc(hidden)]
pub struct ImmutableArguments<DB: Database>(pub DB::Arguments);

impl_into_arguments!($B:
(0) -> T1;
(1) -> T2;
(2) -> T3;
(3) -> T4;
(4) -> T5;
(5) -> T6;
(6) -> T7;
(7) -> T8;
(8) -> T9;
);
impl<DB: Database> IntoArguments<DB> for ImmutableArguments<DB> {
fn into_arguments(self) -> <DB as Database>::Arguments {
self.0
}
}
2 changes: 0 additions & 2 deletions sqlx-core/src/mysql/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,3 @@ impl Database for MySql {

type TableId = Box<str>;
}

impl_into_arguments_for_database!(MySql);
2 changes: 1 addition & 1 deletion sqlx-core/src/postgres/arguments.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use byteorder::{ByteOrder, NetworkEndian};

use crate::arguments::Arguments;
use crate::arguments::{Arguments, IntoArguments};
use crate::encode::{Encode, IsNull};
use crate::io::BufMut;
use crate::types::HasSqlType;
Expand Down
2 changes: 0 additions & 2 deletions sqlx-core/src/postgres/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,3 @@ impl Database for Postgres {

type TableId = u32;
}

impl_into_arguments_for_database!(Postgres);
9 changes: 3 additions & 6 deletions sqlx-core/src/query_as.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use futures_core::Stream;
use futures_util::{future, TryStreamExt};

use crate::arguments::Arguments;
use crate::arguments::{Arguments, ImmutableArguments};
use crate::{
arguments::IntoArguments, database::Database, encode::Encode, executor::Executor, row::FromRow,
types::HasSqlType,
Expand Down Expand Up @@ -128,13 +128,10 @@ where

// used by query!() and friends
#[doc(hidden)]
pub fn bind_all<I>(self, values: I) -> QueryAs<'q, DB, R, I>
where
I: IntoArguments<DB>,
{
pub fn bind_all(self, values: DB::Arguments) -> QueryAs<'q, DB, R, ImmutableArguments<DB>> {
QueryAs {
query: self.query,
args: values,
args: ImmutableArguments(values),
map_row: self.map_row,
}
}
Expand Down
1 change: 0 additions & 1 deletion sqlx-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ uuid = [ "sqlx/uuid" ]
async-std = { version = "1.4.0", default-features = false }
dotenv = { version = "0.15.0", default-features = false }
futures = { version = "0.3.1", default-features = false }
proc-macro-hack = { version = "0.5.11", default-features = false }
proc-macro2 = { version = "1.0.6", default-features = false }
sqlx = { version = "0.1.1", default-features = false, path = "../sqlx-core", package = "sqlx-core" }
syn = { version = "1.0.11", default-features = false, features = [ "full" ] }
Expand Down
46 changes: 27 additions & 19 deletions sqlx-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ extern crate proc_macro;

use proc_macro::TokenStream;

use proc_macro_hack::proc_macro_hack;

use quote::quote;

use syn::parse_macro_input;
Expand All @@ -26,8 +24,22 @@ mod query_macros;

use query_macros::*;

fn macro_result(tokens: proc_macro2::TokenStream) -> TokenStream {
quote!(
macro_rules! macro_result {
($($args:tt)*) => (#tokens)
}
)
.into()
}

macro_rules! async_macro (
($db:ident => $expr:expr) => {{
($db:ident, $input:ident: $ty:ty => $expr:expr) => {{
let $input = match syn::parse::<$ty>($input) {
Ok(input) => input,
Err(e) => return macro_result(e.to_compile_error()),
};

let res: Result<proc_macro2::TokenStream> = task::block_on(async {
use sqlx::Connection;

Expand Down Expand Up @@ -70,40 +82,36 @@ macro_rules! async_macro (
Ok(ts) => ts.into(),
Err(e) => {
if let Some(parse_err) = e.downcast_ref::<syn::Error>() {
return dbg!(parse_err).to_compile_error().into();
macro_result(parse_err.to_compile_error())
} else {
let msg = format!("{:?}", e);
macro_result(quote!(compile_error!(#msg)))
}

let msg = format!("{:?}", e);
quote!(compile_error!(#msg);).into()
}
}
}}
);

#[proc_macro_hack]
#[proc_macro]
pub fn query(input: TokenStream) -> TokenStream {
#[allow(unused_variables)]
let input = parse_macro_input!(input as QueryMacroInput);
async_macro!(db => expand_query(input, db))
async_macro!(db, input: QueryMacroInput => expand_query(input, db))
}

#[proc_macro_hack]
#[proc_macro]
pub fn query_file(input: TokenStream) -> TokenStream {
#[allow(unused_variables)]
let input = parse_macro_input!(input as QueryMacroInput);
async_macro!(db => expand_query_file(input, db))
async_macro!(db, input: QueryMacroInput => expand_query_file(input, db))
}

#[proc_macro_hack]
#[proc_macro]
pub fn query_as(input: TokenStream) -> TokenStream {
#[allow(unused_variables)]
let input = parse_macro_input!(input as QueryAsMacroInput);
async_macro!(db => expand_query_as(input, db))
async_macro!(db, input: QueryAsMacroInput => expand_query_as(input, db))
}

#[proc_macro_hack]
#[proc_macro]
pub fn query_file_as(input: TokenStream) -> TokenStream {
#[allow(unused_variables)]
let input = parse_macro_input!(input as QueryAsMacroInput);
async_macro!(db => expand_query_file_as(input, db))
async_macro!(db, input: QueryAsMacroInput => expand_query_file_as(input, db))
}
11 changes: 6 additions & 5 deletions sqlx-macros/src/query_macros/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn quote_args<DB: DatabaseExt>(
input: &QueryMacroInput,
describe: &Describe<DB>,
) -> crate::Result<TokenStream> {
if input.args.is_empty() {
if input.arg_names.is_empty() {
return Ok(quote! {
let args = ();
});
Expand All @@ -22,7 +22,7 @@ pub fn quote_args<DB: DatabaseExt>(
let param_types = describe
.param_types
.iter()
.zip(&*input.args)
.zip(&*input.arg_exprs)
.map(|(type_, expr)| {
get_type_override(expr)
.or_else(|| {
Expand All @@ -36,7 +36,7 @@ pub fn quote_args<DB: DatabaseExt>(
})
.collect::<crate::Result<Vec<_>>>()?;

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

let args = input.args.iter();
let args = input.arg_names.iter();

Ok(quote! {
let args = (#(&#args),*,);
// emit as a tuple first so each expression is only evaluated once
let args = (#(&$#args),*,);
#args_check
})
}
Expand Down
Loading