-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
217785b
Make proc macros hygienic in bevy_reflect_derive
elbertronnie a9a1ae4
Replace alloc with std
elbertronnie eca2058
Replace Ok with its fully qualified name
elbertronnie 490562a
Use structs instead of local variables
elbertronnie 8d77a90
Add doc to fq_std.rs
elbertronnie 31df182
Move docs at the top
elbertronnie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
//! 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`. | ||
// | ||
//! 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) | ||
//! } | ||
//! ) | ||
//! ``` | ||
|
||
use proc_macro2::TokenStream; | ||
use quote::{quote, ToTokens}; | ||
|
||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.