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

generator: better macro hygiene #192

Merged
merged 7 commits into from
Oct 4, 2024

Conversation

Kijewski
Copy link
Collaborator

@Kijewski Kijewski commented Oct 4, 2024

  • import an alias rinja, so we need less string concatenation at runtime
  • work fine in a #![no_implicit_prelude] context, even if std and/or core are shadowed

This PR is done in anticipation of #191, but IMHO it is also a good change on its own.

Once the new feature is added (which probably will take quite some time), we can replace extern crate #CRATE as rinja with use #macro_input as rinja whereby macro_input is the identifier $crate of the macro.

This change introduces the alias `rinja` that is used for every
integration crate. This way we need less string concatenization at
runtime.
This way, even a shadowed `core` or `std` does not prevent the generated
code from working correctly.
rinja/src/lib.rs Outdated
@@ -67,6 +67,8 @@ mod html;
use std::{fmt, io};

pub use rinja_derive::Template;
#[doc(hidden)]
pub use {core, std};
Copy link
Contributor

Choose a reason for hiding this comment

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

You can refer to core and std by prepending them with :: in macros. Like ::core::whatever. If not, then add a comment to explain why we want it. Also, it's really not great because it's not rare for users to do use rinja::*;.

Copy link
Collaborator Author

@Kijewski Kijewski Oct 4, 2024

Choose a reason for hiding this comment

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

The current code already does that, but even fully-qualified paths like ::core and ::std can be shadowed. See the example in testing.

But yeah, I didn't recognize the problem that use rinja::* would import the symbol core, too, which might be undesirable. Python is smarter than rust in a way: Symbols with a leading underscore are omitted from glob imports. Would be nice if rust did that, too.

I guess I'll simply move the re-exports into the existing #[doc(hidden)] mod helpers. The name already exists, so it can't introduce a new problem.

Copy link
Contributor

Choose a reason for hiding this comment

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

A new module containing it would be better indeed. 👍

@GuillaumeGomez
Copy link
Contributor

Great work, thanks!

@GuillaumeGomez GuillaumeGomez merged commit 441756b into rinja-rs:master Oct 4, 2024
34 checks passed
@Kijewski Kijewski deleted the pr-use-rinja branch October 4, 2024 12:33
@Kijewski
Copy link
Collaborator Author

Kijewski commented Oct 4, 2024

Thank you! And, as usual, thank you for reviewing! :)

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.

2 participants