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

Allow specifying diesel path as derive attribute #4277

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

Elrendio
Copy link
Contributor

@Elrendio Elrendio commented Sep 22, 2024

Resolves #4276

Good to know:

  • Most diffs are from new indentation due to a second argument to wrap_in_dummy_mod
  • I did not add the option on QueryId and the diesel_numeric_ops as these seems to be used in the table! macro and not in derive macros

Might I also suggest picking any import granularity (I however advise One) from the ones that can be automatically formatted by cargo fmt? See here available options

Elrendio added 5 commits September 22, 2024 20:14
…diesel_path_as_derive_attribute

Conflicts:
	diesel_derives/src/identifiable.rs
	diesel_derives/src/queryable.rs
	diesel_derives/src/util.rs
@weiznich weiznich requested a review from a team September 22, 2024 18:47
pub fn wrap_in_dummy_mod(item: TokenStream) -> TokenStream {
pub fn wrap_in_dummy_mod(
item: TokenStream,
diesel_path_override: Option<&syn::Path>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a downside to adding diesel itself to its own prelude instead?
It looks like this would solve the same issue (being able to use diesel as an indirect dependency) without requiring as much more code within diesel_derives, and without requiring more boilerplate in the caller's code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that would work. The import is currently use diesel which means we either need to have a direct dependency called diesel or a module with than name in the consuming crate root. Now assuming that someone want's to build a crate on top of diesel that reexports diesel's derives they would need to require that all consumer crates need to depend on the extension crate and diesel. Reexporting diesel from the extension crate wouldn't help as it needs to be done from the consuming crate.

Copy link
Member

@Ten0 Ten0 Sep 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what you mean.

we either need to have a direct dependency called diesel or a module with than name in the consuming crate root

Side note: it seems that a module won't work anymore.
That was the case at the time the comment in the existing code was written, but now we need extern crate self as diesel;.
That new writing doesn't even seem to require the use anymore so we probably want to update that comment and even remove that use.
That is:

mod y {
	fn f() {
		x::main();
	}
}

extern crate self as x;

fn main() {}

compiles.

But in addition to direct dependency and extern crate in the crate root, we can also resolve things that are explicitly brought in scope, including by a prelude. That is, the following code compiles:

mod y {
	use crate::prelude::*;

	use x;

	use x::main;
}

mod prelude {
	pub use crate as x;
}
fn main() {}

So it looks like adding pub use diesel; to the diesel::prelude would lead to every crate importing the derives via the diesel prelude (which is the most natural way to do so) to also get diesel in scope, in turn making the code expanded by the derives work, because it would then be able to resolve diesel.

This seems to be a simpler way to be able to use diesel as an indirect dependency than what is currently implemented in this PR. (Which is the goal of this PR.)

@Ten0
Copy link
Member

Ten0 commented Sep 23, 2024

Might I also suggest picking any import granularity (I however advise One) from the ones that can be automatically formatted by cargo fmt?

Unless I'm mistaken currently it is "module"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants