-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Introduce a #[declare_sql_function]
attribute macro
#4365
base: master
Are you sure you want to change the base?
Conversation
ed4d25f
to
15d9201
Compare
This commit introduces a new `#[declare_sql_function]` attribute macro that can be applied to `extern "SQL"` blocks. This is essentially the same as the existing `define_sql_function!` function like macro in terms of functionality. I see the following advantages of using an attribute macro + an `extern "SQL"` block instead: * This is closer to rust syntax, so rustfmt will understand that and work correctly inside these blocks * This allows to put several functions into the same block * Maybe in the future this also allows to apply attributes to the whole block instead of to each item The downside of this change is that we then have three variants to declare sql functions: * `sql_function!()` (deprectated) * `define_sql_function!()` (introduced in 2.2, we might want to deprecate that as well?) * The new attribute macro
15d9201
to
7d8df86
Compare
TL;DR: Not initially super enthusiastic, but no strong opinion and open 🙂 Currently in the examples and tests we write things such as: define_sql_function!(fn nextval(a: sql_types::VarChar) -> sql_types::BigInt); Indeed this feels harder to read than #[declare_sql_function]
extern "SQL" {
fn nextval(a: sql_types::VarChar) -> sql_types::BigInt;
} However I actually almost never use the first writing, I rather use this one: define_sql_function! {
fn nextval(a: sql_types::VarChar) -> sql_types::BigInt;
} and that one feels to me like it's as readable as the proposed syntax. My first feeling is that the new syntax may feel more magical and more compiler-integrated than it actually is. Also I wouldn't want to prevent anyone from having this writing if they prefer. :)
I recall seeing rustfmt being able to format code that is used in macros (specifically with |
It shouldn't be too hard to just not have an API there at all given that it's not to specify one in valid rust syntax.
That would be an variant, although I would prefer to have only one variant if possible.
I don't think rustfmt does any macro expansion at all. As far as I know there is just some heuristics that checks if the code is ~rust code and then formats it. For whatever reason that doesn't seem to work with the function like macro, but it works with the marco attribute variant. That written: The main advantage of the new variant is that you can easily declare multiple functions in one block. It becomes much shorter than the older variant in these cases. You can see that in The other motivation for this that while working on #4322 I noticed that we currently don't restrict sql functions to specific backends. That's something we should likely do for many cases (like for all postgres specific functions, etc). If all these functions are placed into a single Finally it might help to speedup the compilation time of diesel while working on diesel. Rustc doesn't cache proc-macro expansions and I think having one large expension is "cheaper" than repeatably calling the same macro again and again, but I've not measured that yet. |
As for the formatting argument: It seems like you can configure rustfmt to format macro calls: https://github.com/rust-lang/rustfmt/blob/master/Configurations.md#skip_macro_invocations That setting is |
I have no strong preference about this. |
This commit introduces a new
#[declare_sql_function]
attribute macro that can be applied toextern "SQL"
blocks. This is essentially the same as the existingdefine_sql_function!
function like macro in terms of functionality.I see the following advantages of using an attribute macro + an
extern "SQL"
block instead:The downside of this change is that we then have three variants to declare sql functions:
sql_function!()
(deprectated)define_sql_function!()
(introduced in 2.2, we might want to deprecate that as well?)For now this is more a request for comments if that's a desirable change. If we decide to go that way I would like to:
define_sql_function!()