-
-
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
reflection: replace impl_reflect_struct
with impl_reflect
#11437
Merged
alice-i-cecile
merged 8 commits into
bevyengine:main
from
soqb:reflect-impl-reflect-nostruct
Jan 30, 2024
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ac2ee03
impl_reflect
soqb 5fbcd1e
doc-fixes & tests
soqb d827ce4
fix doc fixes
soqb 7a6e066
doc fix^3
soqb 00ad1ed
merge & test
soqb ad027ea
merge support
soqb 07e3317
fix test
soqb 07afa74
simplify `type_path_attrs` call
soqb 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
use core::fmt; | ||
|
||
use crate::container_attributes::{FromReflectAttrs, ReflectTraits}; | ||
use crate::field_attributes::{parse_field_attrs, ReflectFieldAttr}; | ||
use crate::type_path::parse_path_no_leading_colon; | ||
|
@@ -140,10 +142,46 @@ enum ReflectMode { | |
Value, | ||
} | ||
|
||
/// How the macro was invoked. | ||
#[derive(Debug, Copy, Clone, PartialEq, Eq)] | ||
pub(crate) enum ReflectImplSource { | ||
ImplRemoteType, | ||
DeriveLocalType, | ||
} | ||
|
||
/// Which trait the macro explicitly implements. | ||
#[derive(Debug, Copy, Clone, PartialEq, Eq)] | ||
pub(crate) enum ReflectTraitToImpl { | ||
Reflect, | ||
FromReflect, | ||
TypePath, | ||
} | ||
|
||
/// The provenance of a macro invocation. | ||
#[derive(Debug, Copy, Clone, PartialEq, Eq)] | ||
pub(crate) struct ReflectProvenance { | ||
pub source: ReflectImplSource, | ||
pub trait_: ReflectTraitToImpl, | ||
} | ||
|
||
impl fmt::Display for ReflectProvenance { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
use self::{ReflectImplSource as S, ReflectTraitToImpl as T}; | ||
let str = match (self.source, self.trait_) { | ||
(S::ImplRemoteType, T::Reflect) => "`impl_reflect`", | ||
(S::DeriveLocalType, T::Reflect) => "`#[derive(Reflect)]`", | ||
(S::DeriveLocalType, T::FromReflect) => "`#[derive(FromReflect)]`", | ||
(S::DeriveLocalType, T::TypePath) => "`#[derive(TypePath)]`", | ||
(S::ImplRemoteType, T::FromReflect | T::TypePath) => unreachable!(), | ||
}; | ||
f.write_str(str) | ||
} | ||
} | ||
|
||
impl<'a> ReflectDerive<'a> { | ||
pub fn from_input( | ||
input: &'a DeriveInput, | ||
is_from_reflect_derive: bool, | ||
provenance: ReflectProvenance, | ||
) -> Result<Self, syn::Error> { | ||
let mut traits = ReflectTraits::default(); | ||
// Should indicate whether `#[reflect_value]` was used. | ||
|
@@ -169,7 +207,7 @@ impl<'a> ReflectDerive<'a> { | |
reflect_mode = Some(ReflectMode::Normal); | ||
let new_traits = ReflectTraits::from_metas( | ||
meta_list.parse_args_with(Punctuated::<Meta, Comma>::parse_terminated)?, | ||
is_from_reflect_derive, | ||
provenance.trait_, | ||
)?; | ||
traits.merge(new_traits)?; | ||
} | ||
|
@@ -184,7 +222,7 @@ impl<'a> ReflectDerive<'a> { | |
reflect_mode = Some(ReflectMode::Value); | ||
let new_traits = ReflectTraits::from_metas( | ||
meta_list.parse_args_with(Punctuated::<Meta, Comma>::parse_terminated)?, | ||
is_from_reflect_derive, | ||
provenance.trait_, | ||
)?; | ||
traits.merge(new_traits)?; | ||
} | ||
|
@@ -264,6 +302,16 @@ impl<'a> ReflectDerive<'a> { | |
|
||
let meta = ReflectMeta::new(type_path, traits); | ||
|
||
if provenance.source == ReflectImplSource::ImplRemoteType | ||
&& meta.traits.type_path_attrs().should_auto_derive() | ||
soqb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
&& !meta.type_path().has_custom_path() | ||
{ | ||
return Err(syn::Error::new( | ||
meta.type_path().span(), | ||
format!("a #[{TYPE_PATH_ATTRIBUTE_NAME} = \"...\"] attribute must be specified when using {provenance}") | ||
)); | ||
Comment on lines
+303
to
+306
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. Awesome! |
||
} | ||
|
||
#[cfg(feature = "documentation")] | ||
let meta = meta.with_docs(doc); | ||
|
||
|
Oops, something went wrong.
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.
Bikeshed: Rather than having to add the
_
suffix, maybe we could rename this totrait_target
(maybe even renamingReflectTraitToImpl
toReflectTraitTarget
) ortrait_to_impl
or something similar.