
Takes the first two commits from #15375 and adds suggestions from this comment: https://github.com/bevyengine/bevy/pull/15375#issuecomment-2366968300 See #15375 for more reasoning/motivation. ## Rebasing (rerunning) ```rust git switch simpler-lint-fixes git reset --hard main cargo fmt --all -- --unstable-features --config normalize_comments=true,imports_granularity=Crate cargo fmt --all git add --update git commit --message "rustfmt" cargo clippy --workspace --all-targets --all-features --fix cargo fmt --all -- --unstable-features --config normalize_comments=true,imports_granularity=Crate cargo fmt --all git add --update git commit --message "clippy" git cherry-pick e6c0b94f6795222310fb812fa5c4512661fc7887 ```
76 lines
2.3 KiB
Rust
76 lines
2.3 KiB
Rust
use crate::{
|
|
container_attributes::ContainerAttributes, derive_data::ReflectTraitToImpl,
|
|
type_path::CustomPathDef,
|
|
};
|
|
use syn::{parenthesized, parse::ParseStream, token::Paren, Attribute, Generics, Path};
|
|
|
|
/// A struct used to define a simple reflection-opaque types (including primitives).
|
|
///
|
|
/// This takes the form:
|
|
///
|
|
/// ```ignore (Method expecting TokenStream is better represented with raw tokens)
|
|
/// // Standard
|
|
/// ::my_crate::foo::Bar(TraitA, TraitB)
|
|
///
|
|
/// // With generics
|
|
/// ::my_crate::foo::Bar<T1: Bar, T2>(TraitA, TraitB)
|
|
///
|
|
/// // With generics and where clause
|
|
/// ::my_crate::foo::Bar<T1, T2> where T1: Bar (TraitA, TraitB)
|
|
///
|
|
/// // With a custom path (not with impl_from_reflect_opaque)
|
|
/// (in my_crate::bar) Bar(TraitA, TraitB)
|
|
/// ```
|
|
pub(crate) struct ReflectOpaqueDef {
|
|
#[cfg_attr(
|
|
not(feature = "documentation"),
|
|
expect(
|
|
dead_code,
|
|
reason = "The is used when the `documentation` feature is enabled.",
|
|
)
|
|
)]
|
|
pub attrs: Vec<Attribute>,
|
|
pub type_path: Path,
|
|
pub generics: Generics,
|
|
pub traits: Option<ContainerAttributes>,
|
|
pub custom_path: Option<CustomPathDef>,
|
|
}
|
|
|
|
impl ReflectOpaqueDef {
|
|
pub fn parse_reflect(input: ParseStream) -> syn::Result<Self> {
|
|
Self::parse(input, ReflectTraitToImpl::Reflect)
|
|
}
|
|
|
|
pub fn parse_from_reflect(input: ParseStream) -> syn::Result<Self> {
|
|
Self::parse(input, ReflectTraitToImpl::FromReflect)
|
|
}
|
|
|
|
fn parse(input: ParseStream, trait_: ReflectTraitToImpl) -> syn::Result<Self> {
|
|
let attrs = input.call(Attribute::parse_outer)?;
|
|
|
|
let custom_path = CustomPathDef::parse_parenthesized(input)?;
|
|
|
|
let type_path = Path::parse_mod_style(input)?;
|
|
let mut generics = input.parse::<Generics>()?;
|
|
generics.where_clause = input.parse()?;
|
|
|
|
let mut traits = None;
|
|
if input.peek(Paren) {
|
|
let content;
|
|
parenthesized!(content in input);
|
|
traits = Some({
|
|
let mut attrs = ContainerAttributes::default();
|
|
attrs.parse_terminated(&content, trait_)?;
|
|
attrs
|
|
});
|
|
}
|
|
Ok(Self {
|
|
attrs,
|
|
type_path,
|
|
generics,
|
|
traits,
|
|
custom_path,
|
|
})
|
|
}
|
|
}
|