-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
[Merged by Bors] - Make proc macros hygienic in bevy_reflect_derive #6752
Changes from 5 commits
217785b
a9a1ae4
eca2058
490562a
8d77a90
31df182
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::{quote, ToTokens}; | ||
|
||
// This module contains unit structs that should be used inside `quote!` and `spanned_quote!` using the variable interpolation syntax in place of their equivalent structs and traits present in `std`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Normally module doc comments are made using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have moved them to the top and used |
||
// | ||
// To create hygienic proc macros, all the names must be its fully qualified form. These unit structs help us to not specify the fully qualified name every single time. | ||
// | ||
// # Example | ||
// Instead of writing this: | ||
// ```ignore | ||
// quote!( | ||
// fn get_id() -> Option<i32> { | ||
// Some(0) | ||
// } | ||
// ) | ||
// ``` | ||
// Or this: | ||
// ```ignore | ||
// quote!( | ||
// fn get_id() -> ::core::option::Option<i32> { | ||
// ::core::option::Option::Some(0) | ||
// } | ||
// ) | ||
// ``` | ||
// We should write this: | ||
// ```ignore | ||
// use crate::fq_std::FQOption; | ||
// | ||
// quote!( | ||
// fn get_id() -> #FQOption<i32> { | ||
// #FQOption::Some(0) | ||
// } | ||
// ) | ||
// ``` | ||
|
||
pub(crate) struct FQAny; | ||
pub(crate) struct FQBox; | ||
pub(crate) struct FQClone; | ||
pub(crate) struct FQDefault; | ||
pub(crate) struct FQOption; | ||
pub(crate) struct FQResult; | ||
|
||
impl ToTokens for FQAny { | ||
fn to_tokens(&self, tokens: &mut TokenStream) { | ||
quote!(::core::any::Any).to_tokens(tokens); | ||
} | ||
} | ||
|
||
impl ToTokens for FQBox { | ||
fn to_tokens(&self, tokens: &mut TokenStream) { | ||
quote!(::std::boxed::Box).to_tokens(tokens); | ||
} | ||
} | ||
|
||
impl ToTokens for FQClone { | ||
fn to_tokens(&self, tokens: &mut TokenStream) { | ||
quote!(::core::clone::Clone).to_tokens(tokens); | ||
} | ||
} | ||
|
||
impl ToTokens for FQDefault { | ||
fn to_tokens(&self, tokens: &mut TokenStream) { | ||
quote!(::core::default::Default).to_tokens(tokens); | ||
} | ||
} | ||
|
||
impl ToTokens for FQOption { | ||
fn to_tokens(&self, tokens: &mut TokenStream) { | ||
quote!(::core::option::Option).to_tokens(tokens); | ||
} | ||
} | ||
|
||
impl ToTokens for FQResult { | ||
fn to_tokens(&self, tokens: &mut TokenStream) { | ||
quote!(::core::result::Result).to_tokens(tokens); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Not super necessary but it would be nice to either have a module-level doc here stating the purpose of these
FQ***
types or doc comments on the individual structs (or both haha).It's pretty simple/self-explanatory so definitely not required, but I like to make sure the barrier to contributing is low for newcomers :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added the doc too.