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

Make derive macros use fully-qualified paths #45

Closed
wants to merge 2 commits into from

Conversation

arctic-hen7
Copy link

This PR makes the macros in this crate use fully qualified paths, which prevents assumptions about what's in the user's scope. For example, this makes it possible to use #[derive(nanoserde::SerJson)] without first writing use nanoserde::SerJson;, which allows this crate to be used in other macros (my requirement).

I think I've caught everything, but there are around a thousand lines of (very impressive) manual macros, so it's entirely possible that I've missed a few things!

By the way, thanks for this crate, it's extremely useful!

@arctic-hen7
Copy link
Author

@not-fl3 when do you think this is likely to be released to crates.io? I don't mean to pressure you, I'm just wondering how I should manage the releases of a project I'd like to integrate this into.

@not-fl3
Copy link
Owner

not-fl3 commented Jul 18, 2022

If I understand correctly, the tradeoff here:

With this PR we disallow using nanoserde with crate rename (like this: notserde = {package = "nanoserde"} use notserde::SerBin;), but allow using #[nanoserde::SerBin]?

Honestly, I do not remember(and can't really find out on the internet fast enough) if this is the only implication here, so I just do not know enough to make a decision on merging this right now :(

}
Some(val)
} else {
if !field.ty.is_option {
Some(String::from("Default::default()"))
Some(String::from("::std::default::Default::default()"))
Copy link
Owner

Choose a reason for hiding this comment

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

afaik Option/Into are pretty safe to use without ::

Copy link
Owner

Choose a reason for hiding this comment

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

also core::option is the better choice to get an Option from. But is it really a necessity to use :: for an Option?

Copy link
Author

Choose a reason for hiding this comment

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

True, but there's no downside, and it's generally recommended to use fully qualified paths everywhere in macros, just in case the user happens to be completely mad.

Copy link
Owner

Choose a reason for hiding this comment

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

any links on a scenario when an Option is not available?

Copy link
Author

Choose a reason for hiding this comment

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

Not links, but the user could be operating in an environment without the standard library's prelude, but with the standard library available (not sure how you'd do that, but I assume you could). What are your reservations?

Copy link
Owner

Choose a reason for hiding this comment

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

yes, but both Option and Into are parts of core, not just std

Copy link
Author

Choose a reason for hiding this comment

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

Oh yes of course, sorry. I don't think that changes anything though, as there are no downsides to having fully-qualified paths for these. I can remove them if you'd like, but I don't see any reason; better safe than sorry, right?

@arctic-hen7
Copy link
Author

Hmm true, I hadn't considered that. As far as I know though, this use of fully qualified paths is just about ubiquitous in other macro crates, so this behavior would be more expected by users than being forced to import things (which I've found very restrictive in using this in a macro of my own that automatically derives SerJson and DeJson --- the user has to either import things that they can't see being used, or they get double-import errors if they want to derive those on other things in their code).

@arctic-hen7
Copy link
Author

@not-fl3 by the way, the user renaming of the crate wouldn't work even without this PR, since there were multiple existing (and necessary) references to nanoserde in the code.

@arctic-hen7
Copy link
Author

@not-fl3 has there been any progress on this PR?

@not-fl3
Copy link
Owner

not-fl3 commented Sep 1, 2022

Honestly, I am not convinced std:: paths make anything better. Especially for types that are actually from core::, not std

And for the nanoserde:: paths, I just do not have an opinion strong enough to either merge it nor not, but the std:: thing gives me a reason to just postpone the decision :)

@arctic-hen7
Copy link
Author

Alright, would you like me to change the std:: paths to core:: paths?

@not-fl3
Copy link
Owner

not-fl3 commented Sep 1, 2022

I would just use them a is, as Vec or Into

Specifically: `Option`, `Into`, `From`, and `AsRef`.
@arctic-hen7
Copy link
Author

I've removed the absolute paths for Option, Into, From, and AsRef. I haven't done so for Result because a lot of people have custom Result types in scope. Let me know if there's anything else you'd like me to change.

@novafacing
Copy link
Contributor

Jumping in here, this is actually quite a blocker for my use case (writing a book where I can't have any top-level use statements). Any chance this request could be revisited?

@novafacing
Copy link
Contributor

I took a swing at this in #124.

@knickish knickish closed this Dec 11, 2024
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.

4 participants